Reputation: 17
I have the following code which displays a dropdown menu (I have it this way the so the form shows the last selected option). I would like to pass the value associated with the selection to a method in my controller.
I want to save this to the the value of "choice" which is specified as an integer in my user model. The initial value of choice is 0.
I can't seem to find a way to do this with the local "value".
<select class="form-control">
<option value= 1>One</option>
<option value= 2>Two</option>
<option value= 3>Three</option>
</select>
<%= link_to "Select", {controller: :controller_name, action: :method_name, value: :value} %>
Controller method (I am using Devise to call "current_user")
def method_name
current_user.choice = value
current_user.save
redirect_to root_path
end
Upvotes: 0
Views: 66
Reputation: 5830
Try a form, here is an example of what you need below:
<%= form_tag(url_for(controller: 'controller_name', action: 'method_name'), method: :method_type(PUT, POST, etc.) do %>
<div class="form-group">
<%= label_tag(:choice, "Choice") %>
<%= select_tag(:choice, options_for_select([0, 1, 2, 3], selected: 0)) %>
</div>
<%= submit_tag 'Save'%>
<% end %>
As a commenter eluded to, in your method you can then use:
def method_name
current_user.choice = params[:choice]
current_user.save
redirect_to root_path
end
Upvotes: 1