将表单路由到Ruby on Rails中的新控制器动作 [英] Route a form to new controller action in Ruby on Rails

查看:75
本文介绍了将表单路由到Ruby on Rails中的新控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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.

我现有的模型和控制器是称为项目,我在控制器中创建了一个名为队列的新动作。目标是用户可以使用 /队列/用户名过滤不同用户的项目。

My existing model and controller is called 'projects', and I created a new action in the controller called 'queue'. The goal is that a user can filter projects by different users using '/queue/username'.

routes.rb

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

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

<%= 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版本 3.2.13

Ruby版本 1.9.3

推荐答案

Josh和Billy的回答可以很好地做到这一点,并为此付出了很多代价。我建议根据项目将其混合使用。假设您的路线比较安静,并且包含的​​项目为资源项目

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. Assuming your routes are restful and contains projects as resources projects:

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

这是在添加路线 projects / queue 到您的路线,因为它基于集合而不是成员。请参阅: http://guides.rubyonrails.org/routing.html#adding -more-restful-actions 获取更多信息。

What this does is add the route projects/queue to your routes since it is based off of the collection instead of a member. See: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions for more info.

值得注意的是为什么会这样。这是因为当您想要的路由是 queue /:id 时,您最初发布的路由仅是 queue id是通过url参数指定的,您应该使用get请求。但是由于您似乎是通过发布请求来发布ID,所以这有点令人困惑。选择仅使用发布请求,不需要像Billy建议的url参数,我也同意,因为这将使您保持简洁。

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. But since it looks like you are posting the id via a post request this is a little confusing. 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.

希望这会有所帮助!

编辑用于用户名使用

将表单更改为:

<%= 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 %>

假设JS正常运行,您只需要用<$填充隐藏字段c $ c>:username 值,而不是:id 。然后在控制器方面:

Assuming the JS is working as it should, you just need to populate the hidden field with the :username value instead of :id. Then on the controller side of things:

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

希望这很有意义!

这篇关于将表单路由到Ruby on Rails中的新控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆