简体   繁体   English

Rails-将表单指向控制器动作

[英]Rails - Pointing form to controller action

I have a simple controller "Users" which has a show action (for each user). 我有一个简单的控制器“ Users”,具有一个show动作(针对每个用户)。 Now, I want to have a textbox on the home page which allows to search for a specific user by username. 现在,我想在主页上有一个文本框,该文本框允许按用户名搜索特定用户。

So on show action, I accept a username param and I respond with the user object. 因此,在表演动作中,我接受用户名参数,并以用户对象作为响应。 I've seen form_for() but it seems I need an instance of @user which I don't quite understand, since I am only just retrieving on the next request. 我看过form_for(),但似乎我需要一个我不太了解的@user实例,因为我只是在获取下一个请求。 I've seen tutorials but they seem to have it under index controller action but I really want it to point to users#show so it will use the assets, template, url path, etc. 我看过教程,但是他们似乎在索引控制器的作用下拥有它,但我真的希望它指向users#show,以便它将使用资产,模板,URL路径等。

So I want a form that would just perform a get request with /users/. 因此,我想要一种仅使用/ users /执行get请求的表单。 Am I just missing something simple? 我只是想念一些简单的东西吗?

You don't need instance if you use form_tag . 如果使用form_tag则不需要实例。 You can read more about form_tag here 您可以在此处阅读有关form_tag更多信息

Example: 例:

<%= form_tag("/search", method: :get) do %>
  <%= text_field_tag :user %>
  <%= submit_tag "Search" %>
<% end %>

The html snippet from @Mihail well help you but the example given will require you to create a custom route for the search action. @Mihail的html片段可以很好地帮助您,但是给出的示例将要求您为搜索操作创建自定义路由。

Add in your routes: 添加您的路线:

GET '/search', to: 'users#show'

Then in your users show action attempt to look up the user by calling: 然后在您的用户中显示操作,尝试通过调用以下命令来查找用户:

@user = User.find_by(id: params[:user])

Be sure to not use User.find , because you will get an error when the user doesn't exist. 确保不使用User.find ,因为当用户不存在时,您将得到一个错误。 The last thing you'll need to do is make sure your users/show template is able to handle the case when @user is nil, or instead render some sort of custom user_not_found page instead of the users/show if @user is nil. 您需要做的最后一件事是,确保@user为nil时,用户/显示模板能够处理这种情况,或者如果@user为nil,则呈现某种自定义user_not_found页面,而不是用户/ show。

You can use form_tag instead of form_for . 您可以使用form_tag而不是form_for Also, all the form helpers have their _tag equivalents. 另外,所有表单助手都有其_tag等效项。 Ie select_tag instead of f.select and so on. select_tag而不是f.select等。 And don't forget the case when user could not be found, You have to deal with it somehow. 并且不要忘记找不到用户的情况,您必须以某种方式处理它。

I'll just add in what I did. 我只是添加我所做的。

As advised, I've added a route for /search on config/routes.rb. 根据建议,我在config / routes.rb上为/ search添加了一条路由。

get '/search', to: 'users#search'

Then on UsersController, I've added a redirect on search action. 然后在UsersController上,我在搜索操作上添加了重定向。

  def search
    @username = params[:search]
    redirect_to "#{users_path}/#{@username}"
  end

This would now allow me to reuse whatever I have under users#show (templates, url and everything). 现在,这将允许我重用users#show下的所有内容(模板,URL和所有内容)。 From what I understand, this will return a 301/302 that would cause another HTTP request so this is not at all optimal. 据我了解,这将返回301/302,这将导致另一个HTTP请求,因此这并不是最佳选择。 It would probably be cleaner to just share functionality between show and search. 仅在显示和搜索之间共享功能可能会更清洁。

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

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