问题
i have the follow
<%= form_tag lessons_path, :method => 'get', :name => "filter_form" do %>
<div class="btn-group" data-toggle-name="is_private" data-toggle="buttons-radio" >
<button value="0" class="btn" data-toggle="button">Public</button>
<button value="1" class="btn" data-toggle="button">Private</button>
</div>
when a user clicks on either of those buttons, the form submits and goes to my lessons#index. how can i get what value the user clicked on? i understand that params[:something] usually comes from a GET request in the url, but what about in this case?
thanks
回答1:
Just give your buttons name
attributes:
<%= form_tag lessons_path, :method => 'get', :name => "filter_form" do %>
<div class="btn-group" data-toggle-name="is_private" data-toggle="buttons-radio" >
<button name="public" type="submit" value="0" class="btn" data-toggle="button">Public</button>
<button name="private" type="submit" value="1" class="btn" data-toggle="button">Private</button>
</div>
<% end %>
and then check params
as usual in your controller:
if(params[:public])
# public has pressed
elsif(params[:private])
# private was pressed
else
# Hmm, someone is trying to cause trouble.
end
You'll note that I added type="submit"
to the <button>
s, some browsers use type="submit"
as the default (as per the standard) and some use type="button"
so it is best to be explicit.
回答2:
In rails, the params hash will contain all the elements that have been submitted/passed to the page. This includes GET parameters and POST parameters. You should be using a check box (or perhaps radio buttons) to represent this type of data. If your field exists on the database, then you should do the following:
<br><%= f.check_box :is_private %> Private
You can then access the data with params[:is_private]
If you want to use buttons, then I Would recommend creating two urls to toggle the state of the check box. Otherwise you will have to depend on JavaScript to change a field.
If you want to use buttons, but also use the same update url, then you will need to have the button presses toggle the state of the check box there are JQuery plugins that can do this (one example is http://widowmaker.kiev.ua/checkbox/).
回答3:
A form will only submit to one action.
Use i18n to define your button text then check the i18n value of value of the commit param in the controller action
e.g. in your form
<%= submit_tag I18n.t('checkout.button.place_order'), :class => "submit" %>
The same will work for a f.submit button.
then in your controller
if params[:commit] == I18n.t('checkout.button.place_order')
Do something
else
Do something else
end
The in your en.yml or whatever to define the actual text
en:
checkout:
button:
place_order: 'Place Order'
Obviously just adjust to suit your needs
By using i18n, you can easily change the text on your buttons in the i18n definition without having any effect on your controller logic and will still work on any internationalisation/translations you wish to use
Using this you can set up as many buttons on a single form as you like then add whatever conditional logic you need in the controllers action
UPDATE I18n is short for internationalisation (18 chars in the word :)), just create a file in the locale folder called en.yml then define whatever you need in the format that I described above and you are good to go. Reference I18n variables anywhere you need them using the t. helper Have a look at this railscast for more detailed explanation http://railscasts.com/episodes/138-i18n
Update 2 In response to mu is too short's comment below
Why not just add a name attribute? Then you can work with what the button independent of its content. Keep in mind that a can contain embedded HTML that won't make much sense inside param
using i18n makes this comment totally invalid as the text is not checked at all. You can make the text whatever you want and indeed it is good practice to use I18n. Note that you are checking the constant in the controller 'checkout.button.place_order' in the above example NOT the value which is defined as 'Place Order'.
来源:https://stackoverflow.com/questions/10309664/rails-and-html-how-to-retrieve-params-from-a-form