Ruby on Rails 脚手架 - 修改显示方法
我是 Ruby on Rails 的初学者,所以我需要一些帮助。我最近开始阅读基础教程,该教程是使用脚手架教授的。我制作了一个“客户端”模型: script/generatescaffoldclients name:string ip_address:string speed:integer ...在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
对于查询,我会转到localhost:3000 /clients/{在此输入 ID}。我不想用参数搜索 ID,而是想用另一个值搜索,比如 ip_address 或 speed,所以我想我所要做的就是将 :id 更改为 :ip_address in "@client = Client.find(参数[:id])”。但是,这不起作用,所以请有人告诉我如何使用另一个参数实现搜索。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于路由的方式,这不起作用
之类的操作时
当您执行诸如
map.resources :client
(请参阅
config/routes.rb
) ,当您使用脚手架。它根据您使用 id 的假设来设置路由。
其中一条路由类似于
map.connect 'clients/:id', :controller => '客户端', :action => 'show'
因此
:id
作为参数作为 URL 的一部分传递。您不应该将 IP 作为主要标识符,除非它们是不同的 - 即使这样,它也会与 RESTful 路由产生混乱。
如果您希望能够按 IP 进行搜索,请修改客户端的索引操作
然后您可以通过转到
clients?ip=###.###.###
按 ip 进行搜索This doesn't work because of the way things are routed
When you do something like
map.resources :client
(Seeconfig/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
Then you can search by ip by going to
clients?ip=###.###.###
routes.rb 文件中的这一行
意味着,当调度程序使用 GET 方法接收到格式为“clients/abcdxyz”的 URI 时,它将重定向到显示方法,其值“abcdxyz”可在 params 哈希中使用键 :id 获得。
编辑
由于您使用了脚手架,客户端资源将是 RESTful,这意味着当您向“/clients/:id”URI 发送 GET 请求时,您将被重定向到显示该特定客户端的页面。
在您的控制器代码中,您可以将其访问为
通过在主键(即“id”列)上进行脚手架搜索而生成的 find 方法。您需要将该语句更改为
OR
:-)
This line in your routes.rb file
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.
EDIT
Since you have used scaffold, the clients resource will be 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.
In your controller code you can access it as
The find method that is generated by scaffold searches on the primary key i.e 'id' column. You need to change that statement to either
OR
:-)