Ruby on Rails 脚手架 - 修改显示方法

发布于 2024-09-07 03:33:31 字数 638 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-09-14 03:33:31

由于路由的方式,这不起作用

之类的操作时

当您执行诸如map.resources :client

(请参阅config/routes.rb) ,当您使用脚手架。
它根据您使用 id 的假设来设置路由。

其中一条路由类似于

map.connect 'clients/:id', :controller => '客户端', :action => 'show'

因此 :id 作为参数作为 URL 的一部分传递。

您不应该将 IP 作为主要标识符,除非它们是不同的 - 即使这样,它也会与 RESTful 路由产生混乱。


如果您希望能够按 IP 进行搜索,请修改客户端的索引操作

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

然后您可以通过转到 clients?ip=###.###.### 按 ip 进行搜索

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=###.###.###

作妖 2024-09-14 03:33:31

routes.rb 文件中的这一行

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

意味着,当调度程序使用 GET 方法接收到格式为“clients/abcdxyz”的 URI 时,它将重定向到显示方法,其值“abcdxyz”可在 params 哈希中使用键 :id 获得。

编辑


由于您使用了脚手架,客户端资源将是 RESTful,这意味着当您向“/clients/:id”URI 发送 GET 请求时,您将被重定向到显示该特定客户端的页面。


在您的控制器代码中,您可以将其访问为

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

通过在主键(即“id”列)上进行脚手架搜索而生成的 find 方法。您需要将该语句更改为

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

OR

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

:-)

This line in your routes.rb file

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.

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

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

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

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

OR

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

:-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文