Rails And Html, How To Retrieve Params From A Form
and then check params
as usual in your controller:
if(params[:public])
# public has pressedelsif(params[:private])
# private was pressedelse# 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.
Solution 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/).
Solution 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
elseDo something elseend
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'.
Post a Comment for "Rails And Html, How To Retrieve Params From A Form"