开发者

Routes and primary keys in rails

开发者 https://www.devze.com 2022-12-17 05:52 出处:网络
I am a rails rookie and fear that my question is woefully ignorant - please be gentle. I am building an app that will catalog and monitor other websites, and need to synchronize with an external data

I am a rails rookie and fear that my question is woefully ignorant - please be gentle.

I am building an app that will catalog and monitor other websites, and need to synchronize with an external data source (thus overiding primary key).

I have the model below, but i cannot fig开发者_运维知识库ure out the routing for accessing individual records (tho list works)

any pointers?

class Affiliate < ActiveRecord::Base

require 'resolv'
  require 'rubygems'
  require 'rwebthumb'
  include Simplificator::Webthumb
  self.primary_key = "domain"
  validates_uniqueness_of :domain
  validates_presence_of [:org_name, :url], :message => "can't be blank"

  after_create :generate_thumbnail 

  def to_param
    id.chomp.gsub(".","")
  end


  def generate_thumbnail
    wt = Webthumb.new(APP_CONFIG['webthumb_api']['key'])
    job = wt.thumbnail(:url => url)
    job.write_file(job.fetch_when_complete(:medium2), "#{RAILS_ROOT}/public/data/#{id}.png")
  end

  def thumbnail_url
    "/data/#{id}.png"
  end

  def thumbnail_localfile
    File.exist?("#{RAILS_ROOT}/public/data/#{id}.png") ? "#{RAILS_ROOT}/public/data/#{id}.png" : nil
  end

  def thumbnail_last_updated
    if thumbnail_localfile
      File.new(thumbnail_localfile).mtime.to_s(:long)
    else
      "No such file"
    end
  end


end

update: Thank you both!

It turns out the issue was the ordering of routes in my routes.rb


If you generated the Affiliates model by using the scaffold generator then you should be able to access individual affiliates by doing http://localhost:3000/Affiliates/show/

If this does not work, try specifying a route in routes.rb in your projects config directory.
There you can associate a url to an action and then writing the action yourself. Entry for routes.rb: map.connect 'affiliates/:id', :controller => 'affiliates', :action=> 'show'

and then a simple function in the controllers to return the affiliate

function in .rb : def show affiliate = Affiliate.find_by_id(params[:id]) end

Now the affiliate variable will contain attributes of the affiliate identified by :id


Stack Overflow responders are rarely mean, even if they are their posts are quickly edited by someone with higher rank.

To get a list of all of your routes you can navigate to your rails directory in terminal and then issue the command:

rake routes

This will spit out all of the routes in your application. If you created the model using a scaffold generator you can normally get to it by going to http://localhost:3000/:model_name/:action_name/:id . So if your model was named Affiliate, and your action was to show a Affiliate, and you wanted to show the Affiliate with an ID of 22. You would go to http://localhost:3000/Affiliate/show/22 . I hope this helps, if not please follow up with comments to the answer, i understand that getting started with something new even learning the right questions to ask can be a challenge in and of itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消