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 version 3.2.13
Ruby version 1.9.3