简体   繁体   English

将表单路由到Ruby on Rails中的新控制器操作

[英]Route a form to new controller action in Ruby on Rails

I am relatively new to Ruby on Rails, and am trying to set up a form with a new action on an existing controller. 我对Ruby on Rails相对较新,我正在尝试在现有控制器上设置一个带有新操作的表单。

My existing model and controller is called 'projects', and I created a new action in the controller called 'queue'. 我现有的模型和控制器称为“项目”,我在控制器中创建了一个名为“queue”的新操作。 The goal is that a user can filter projects by different users using '/queue/username'. 目标是用户可以使用“/ queue / username”过滤不同用户的项目。

routes.rb 的routes.rb

match 'queue/:username' => 'projects#queue'

projects_controller.rb projects_controller.rb

  def queue
    if (params[:user_id].blank? && params[:user_id].nil?)
      @projects = Project.find_all_by_user_id(User.where(:username => params[:username]).pluck(:id))
    else
      @projects = Project.find_all_by_user_id(params[:user_id])
    end
  end

queue.html.erb queue.html.erb

<%= form_tag("queue", :id => "select_user", :method => "post") do %>
    <%= hidden_field_tag('user_id', '') %>
    <%= text_field_tag('user', nil, :placeholder => 'Enter a user...', class: "users",
                       data: {autocomplete_source: User.order(:lastname, :firstname).map { |u| {:label => u.firstname + " " + u.lastname, :id => u.id} }}) %>
<% end %>

When I submit this form it submits as 'queue/queue', and in order to have a direct link to this action I need to do: 当我提交此表单时,它会提交为“队列/队列”,为了直接链接到此操作,我需要执行以下操作:

<%= link_to "Queue", queue_path + "/" + current_user.username.to_s %>

Which I know is not correct. 我所知道的不正确。

My question is how do I get the form to submit as 'queue/username'? 我的问题是如何将表单提交为“队列/用户名”? Should this be in a new 'queue' controller to handle routing separately? 这应该在一个新的“队列”控制器中单独处理路由吗? Any help is appreciated. 任何帮助表示赞赏。

Rails version 3.2.13 Rails版本 3.2.13

Ruby version 1.9.3 Ruby版本 1.9.3

Two places to revise: 两个地方修改:

  1. Route. 路线。 Better to use static route for POST without parameter, and specify POST 最好在没有参数的情况下使用静态路由进行POST,并指定POST

     match 'projects/queue' => 'projects#queue', via: :post 
  2. Form tag. 表格标签。 You need to specify the path 您需要指定路径

     <%= form_tag "projects/queue", method: :post do %> 

    Better not to use div id, if you have to, use it like this 最好不要使用div id,如果必须,请像这样使用它

     <%= form_tag "projects/queue", method: :post, html: {id: "select_user"} do %> 

You can change your route to something like 你可以改变自己的路线

match 'queue/:username' => 'projects#queue', :as => 'projects_queue'

And then have the destination be 然后有目的地

projects_queue(current_user)

The answers from Josh and Billy can accomplish this well, to throw another hat into the mix I would suggest making this a route based off of the projects . Josh和Billy的答案可以很好地完成这个任务,并将另外一个帽子投入到混合中,我建议将其作为基于projects的路线。 Assuming your routes are restful and contains projects as resources projects : 假设您的路线是宁静的并且包含项目作为resources projects

resources :projects do 
   post "queue", :on => :collection
end

What this does is add the route projects/queue to your routes since it is based off of the collection instead of a member. 这样做是将路由projects/queue添加到您的路由,因为它基于集合而不是成员。 See: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions for more info. 有关详细信息,请参阅: http//guides.rubyonrails.org/routing.html#adding-more-restful-actions

It is worth noting why this happens. 值得注意的是为什么会发生这种情况。 It is because the the route you are originally posting to is only queue when what you want is queue/:id since the id is specified via a url parameter you should use a get request. 这是因为您最初发布的路由只是queue当您想要的是queue/:id因为id是通过url参数指定的,您应该使用get请求。 But since it looks like you are posting the id via a post request this is a little confusing. 但由于看起来你是通过帖子请求发布id,这有点令人困惑。 Choosing just to use a post request you do not need the url parameter like Billy suggested and I also agree since this will allow you to keep it a but more succinct. 选择只是使用发布请求你不需要像Billy建议的url参数,我也同意,因为这将允许你保持它更简洁。

Hope this helps! 希望这可以帮助!

Edit For username usage 编辑用于用户名

Changing your form to: 将表单更改为:

<%= form_tag("queue", :id => "select_user", :method => "post") do %>
    <%= hidden_field_tag(:username) %>
    <%= text_field_tag('user', nil, :placeholder => 'Enter a user...', class: "users",
                       data: {autocomplete_source: User.order(:lastname, :firstname).map { |u| {:label => u.firstname + " " + u.lastname, :username => u.username} }}) %>
<% end %>

Assuming the JS is working as it should, you just need to populate the hidden field with the :username value instead of :id . 假设JS正常工作,您只需要使用:username值而不是:id填充隐藏字段。 Then on the controller side of things: 然后在控制器方面:

  def queue
    @projects = Project.find_all_by_username(params[:username])
  end

Hope this makes sense! 希望这是有道理的!

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

相关问题 Rails 3-新的控制器动作路线 - Rails 3 - new controller action route Ruby on Rails将通配符子域路由到控制器/操作 - Ruby on Rails route for wildcard subdomains to a controller/action 将表单指向导轨上的特定控制器动作红宝石 - Point a form to a specific controller action ruby on rails Ruby On Rails自定义路由始终重定向到控制器的show动作 - Ruby On Rails custom route always redirects to controller's show action Ruby on Rails路由错误:没有路由匹配{:action =&gt;“update”,:controller =&gt;“users”} - Ruby on Rails Routing error: No route matches {:action=>“update”, :controller=>“users”} 如何在Ruby on Rails中添加自定义路由,控制器和操作? - How to add a custom route, controller, and action in Ruby on Rails? 没有路线匹配{:action =&gt;“ show”,:controller =&gt;“ pythonhowto”} - ruby on rails No route matches {:action=>“show”, :controller=>“pythonhowto”} Ruby on Rails:使用controller,action和param获取路由 - Ruby on Rails : get route using controller, action & param Ruby On Rails - 路由错误 - 没有路由匹配{:action =&gt;“show”,:controller =&gt;“trips”} - Ruby On Rails - Routing Error - No route matches {:action=>“show”, :controller=>“trips”} Rails 3-进行了新的控制器动作和查看,但无法进行布线 - Rails 3 - Made a new controller action and view but cant route it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM