0

I am a beginner when it comes to Ruby on Rails, so I need a little bit of help. I started reading a basic tutorial recently, which was taught using Scaffolding. I made a "Clients" model: script/generate scaffold clients name:string ip_address:string speed:integer ... Inside the clients_controller.rb file, there is a method called show:

  # GET /clients/1
  # GET /clients/1.xml
  def show
    @client = Client.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @client }
    end
  end

For queries, I'd go to localhost:3000/clients/{Enter the ID here}. Instead of searching with the ID are the argument, I'd like to search with another value, like ip_address or speed, so I thought all I would have to do is change :id to :ip_address in "@client = Client.find(params[:id])". However, that does not work, so would someone please tell me how I can achieve a search with another parameter. Thanks!

4

2 に答える 2

1

This doesn't work because of the way things are routed

When you do something like

map.resources :client (See config/routes.rb)

This happens automatically when you use scaffold. It sets up routes based on the assumption you're using an id.

One of these routes is something like

map.connect 'clients/:id', :controller => 'client', :action => 'show'

So :id is passed as a parameter as part of the URL.

You shouldn't have the IP be the primary identifier unless they're distinct - and even then it kind of messes with the RESTful routing.


If you want to have the ability to search by IP, modify your index action for the clients

def index
  if params[:ip].present?
    @clients = Client.find_by_ip_address(params[:ip]);
  else
    @clients = Client.all
  end
end

Then you can search by ip by going to clients?ip=###.###.###

于 2010-06-25T02:46:04.513 に答える
0

ルート.rbファイルのこの行

map.connect 'clients/:id', :controller => 'client', :action => 'show'

ディスパッチャがGETメソッドで「clients/abcdxyz」の形式のURIを受信すると、キー:idのparamsハッシュで使用可能な値「abcdxyz」のメソッドを表示するようにリダイレクトすることを意味します。

編集


スキャフォールドを使用したので、クライアントリソースはRESTfulになります。つまり、GETリクエストを「/ clients /:id」URIに送信すると、その特定のクライアントのページを表示するようにリダイレクトされます。


コントローラコードでは、次のようにアクセスできます

params[:id] # which will be "abcdxyz"

主キー、つまり「id」列でのスキャフォールド検索によって生成されるfindメソッド。そのステートメントを次のいずれかに変更する必要があります

@client = Client.find_by_ip_address(params[:id]) #find_by_column_name

また

@client = Client.find(:first, :conditions => [":ip_address = ?", params[:id]])

:-)

于 2010-06-26T04:15:08.673 に答える