简体   繁体   English

Ruby on Rails支架-修改显示方法

[英]Ruby on Rails Scaffold - Modify Show Method

I am a beginner when it comes to Ruby on Rails, so I need a little bit of help. 我是Ruby on Rails的初学者,因此我需要一点帮助。 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: 我创建了一个“客户端”模型:脚本/生成脚手架客户端名称:字符串ip_address:字符串速度:整数...在clients_controller.rb文件中,有一个名为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}. 对于查询,我将转到localhost:3000 / clients / {在此处输入ID}。 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])". 我不想使用ID来搜索参数,而是想使用另一个值来搜索,例如ip_address或speed,所以我认为我所要做的就是将“ @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! 谢谢!

This doesn't work because of the way things are routed 由于事情的路由方式,这不起作用

When you do something like 当你做类似的事情

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

This happens automatically when you use scaffold. 使用脚手架时,这会自动发生。 It sets up routes based on the assumption you're using an id. 它基于您使用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. 因此, :id作为参数传递为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. 除非它们是不同的,否则不应该将IP作为主要标识符-即便如此,它也会与RESTful路由混淆。


If you want to have the ability to search by IP, modify your index action for the clients 如果您希望能够按IP进行搜索,请修改客户端的索引操作

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=###.###.### 然后,您可以通过转到clients?ip=###.###.###通过ip进行搜索clients?ip=###.###.###

This line in your routes.rb file 您的routes.rb文件中的这一行

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

implies that when the dispatcher receives an URI in the format "clients/abcdxyz' with GET Method, it will redirect it to show method with the value "abcdxyz" available in params hash with key :id. 表示当调度程序通过GET方法接收到格式为“ clients / abcdxyz”的URI时,它将使用键:id在params hash中将其重定向为显示值为“ abcdxyz”的方法。

EDIT 编辑


Since you have used scaffold, the clients resource will be RESTful. 由于您已经使用了脚手架,因此客户端资源将是RESTful的。 That means that when you send a GET request to "/clients/:id' URI, you will be redirected to show page of that particular client. 这意味着,当您向“ / clients /:id” URI发送GET请求时,您将被重定向到该特定客户端的显示页面。


In your controller code you can access it as 在您的控制器代码中,您可以按以下方式访问它

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

The find method that is generated by scaffold searches on the primary key ie 'id' column. 脚手架生成的find方法在主键(即“ id”列)上进行搜索。 You need to change that statement to either 您需要将该语句更改为

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

OR 要么

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

:-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM