开发者

In Rails, how to create a dropdown menu that fills in parameters to a path to navigate to?

Trying to create a selec开发者_运维知识库t menu with items from a collection, so that upon selection of an item, and hitting submit, the user is taken to the "Show" action for that item...What I have is something like this:

<% form_tag("subjects/#{@subject.id}/state/:id", :method=>:get) do %>
  <%= select_tag('state', options_from_collection_for_select(State.states, 'id', 'name'))%>
  <%= submit_tag "go!" %>
<% end %>

What'd I like is for what's selected in the menu to fill in the :id parameter...(this is rails 2.3)


You can send the form to an action that redirect where you want:

<% form_tag("some_controller/redirection", :method=>:get) do %>
  <%= select_tag('id', options_from_collection_for_select(State.states, 'id', 'name'))%>
  <%= hidden_field_tag :subject_id, @subject.id %>
  <%= submit_tag "go!" %>
<% end %>

in SomeController

def redirection
  redirect to "subjects/#{#{params[:subject_id]}}/state/#{params[:id]}"
end


The structure of the URL that you are requesting can't be made using only an HTML form. It will require some Javascript:

$('#my_form').submit(function(){
  window.location = '/subjects/' + $('#subject_id').val() + '/state/' + $('#state_id').val();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜