I have a question about forms. I have a fairly standard form that saves a post (called an eReport in my app) with a title and body. The table also has a "published" field, which is boolean. The saved eReport only shows on the public site if this field is set to true, with false being the default.
Rather than the default check box, I would like to display two buttons at the end of the form: a "Publish Now" button and a "Save as Draft" button. If the user presses the former, the published field would be set to true. If the latter, then false. In PHP, I used to display 2 submit fields with different name values, then handle the input with an if/else statement to determine the proper SQL query to build. In Rails, I'm assuming I would place this logic in the controller, under the appropriate action, but I'm not sure how to manipulate the name or id values of buttons.
For the record, I'm using Formtastic, but if someone could show me how to do this with the default Rails form tags, that's OK too. Here's the code for my form as it stands right now:
<% semantic_form_for #ereport do |form| %>
<% form.inputs do %>
<%= form.input :title %>
<%= form.input :body %>
<% end %>
<% form.buttons do %>
<%= form.commit_button :label => "Publish Now" %>
<%= form.commit_button :label => "Save as Draft" %>
<% end %>
<% end %>
Thanks in advance for the help!
I don't know about formtastic, but with the default rails form builder, you could do it like this:
<%= form.submit "Save with option A", :name => "save_option_a" %>
<%= form.submit "Save with option B", :name => "save_option_b" %>
Then in the controller, you can pick those up in params:
if params[:save_option_a]
# do stuff
end
in addition to #iddlefingers answer, here is a view of the log of the application (striping some useless params due to explanation purposes)
Parameters: {"utf8"=>"✓", ..., "comentar"=>"Confirmar"}
where we can see that comentar is the name of the parameter, and "Confirmar" is it's value, which is the button's text too.
which was obtained by submit_tag "Confirmar", :name => 'comentar'
So in general you could have (if you want to reduce the number of params you are working with) several submit_tag "onevalue", :name => 'SAMEname', submit_tag "othervalue", :name => 'SAMEname'...
and retrieve them in your controller
if params[:SAMEname] == "onevalue"
# do stuff
elsif params[:SAMEname] == "othervalue"
#do different stuff
end
I think you need to use jQuery.
You can bind the button click event and submit the form for specified location.
Related
I need a way for my form to not be sent if the user didn't bother to select any radio buttons.
I'd like to to that within the view and the controller, not in the model (the data shouldn't even be sent)
<%= form_tag("/bookings/new", method: "get") do %>
<% #flights.each do |flight| %>
<%= radio_button_tag :flight_id, flight.id %>
<% end %>
<%= submit_tag "book now" %>
<% end %>
edit, to clarify
normally I'd do
<%= f.text_field :name, required: true %>
but, as I have many radio buttons and I only need one for the form to work, I don't know how to implement it
You can set validation in the model to see the presence of checkbox if javascript is disabled. This is a more robust method.
validates :flight_id, :acceptance => true
Docs here - http://guides.rubyonrails.org/active_record_validations.html#acceptance
Edit
function validateCheckBox() {
var x = document.getElementById("flight_id").checked;
if(!x) {alert("Not checked")}
}
<%= submit_tag "book now" , :onclick => "validateCheckBox();" %>
<%= f.text_field :name, required: true %>
This still works perfectly for radio buttons, and it's okay if it ends up on all radio items. The form will still only require one input.
I just tested it on my Rails 6 app.
i have a form in my index-view where i create multiple checkboxes. One checkbox for every entry. This looks like this:
index.html.erb
<%= form_for :user, url: usersupdate_path() do |f| %>
<%= render #users %>
<%= f.submit 'test', :class => 'btn btn-primary' %>
<% end %>
_user.html.erb
<%= check_box_tag "checked[#{user.id}]","#{user.id}",true %>
Description:
With the form i want to allow the admin to uncheck users - this users i want to send to the controller and update their attributes.
There are only 2 problems:
1) I have to refresh the site until i can send the form to the controller - i don't know why
2) When i print the array it looks like this:
{"1"=>"1", "2"=>"2", "4"=>"4"}
User 3 was unchecked by me.
What i want is something like this:
{"1"=>"true", "2"=>"true", "3"=>"false", "4"=>"true"}
But how can i send the checked value of the checkbox to the controller?
In my controller i do only this at the moment:
def update
flash[:success] = params[:checked]
redirect_to root_path
end
Thanks
The browser does not serialize an unchecked checkbox when sending form data, so if it is not checked, it never gets sent.
You can generally fix this two ways. Make your action smart enough to see "missing" values as "unchecked", or add a hidden field before each checkbox:
<%= hidden_field_tag "checked[#{user.id}]", "false" %>
<%= check_box_tag "checked[#{user.id}]","#{user.id}", true %>
As for the true-values, the second parameter to check_box_tag is the value you want the checkbox to have, so you can change it to this:
<%= hidden_field_tag "checked[#{user.id}]", "false" %>
<%= check_box_tag "checked[#{user.id}]","true", true %>
And it should do what you want.
Note that if you use FormBuilders they handle this nuance for you.
My code is as below :
<% for market_language in #pyr_market_languages %>
<%= f.input :"pyr_crm_call_script[market_language_ids][]", :as => :check_box, input_html:{value: market_language.id}, #callscript.market_languages.include?(market_language) %>
<%= market_language.name %><br/>
<% end %>
I just want to use simple form tag for checkbox like f.input,because i couldn't see the errors for checkbox field even if it is mandatory.
Can any one of you provide the way of using checkbox in a simple form in rails.
My form has two radio buttons, public and private:
<div class='review-form'>
<%= simple_form_for(#review) do |f| %>
# input boxes for current_user to put text here
#'public' radio button, checked by default, class is for css
<%= f.radio_button :visible, "true" , :class => "share_button" %>
#'private' radio button, class is for css
<%= f.radio_button :visible, "false", :class => "keep_private_button" %>
#user can cancel
<%= link_to I18n.t('write_review.cancel_button'), landing_page,
:class => 'btn' %>
#user can submit
<%= f.button :submit, I18n.t('write_review.submit_button'),
:class => 'btn btn-primary' %>
How can I determine if the radio button in each review is true or false, so I can use it after the review has been saved in my app?
For example, something like:
<% if #review.radio_button.value = true %>
<%= label_tag("This review is public") %>
<% end %>
<% if #review.radio_button.value = false %>
<%= label_tag("This review is private") %>
<% end %>
Where is "elsewhere?" Is this after the user has saved? If so, then you should just be able to say:
if #review.visible?
or
if review.visible?
depending on the scope of your variable.
If, by "elsewhere," you mean on the same page, then you'll need some clever javascript. You can use something like jQuery to help here, but essentially you'll just want a listener on those radio buttons that, on click, fires off to a javascript function that evaluates the radio buttons, then updates the text of a div on the page appropriately. It's a very common pattern that I'll leave as an exercise for the alert reader, as, again, I'm not sure exactly what you mean by "elsewhere" -- from your question, though, I'm guessing it's the former (after the user has saved).
Hope that helps!
I got a list page and I filter items via links with get params (I can choose many links so query would be like "?param1=value1¶m2=value2"). But also I have to filter it by text field, so I made a form:
<form>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
</form>
But when I submit it, text field param replaces existing query params. So, how to make text field value add to query, not to replace it?
Since I was just dealing with this problem in Rails 4 I thought I'd share my solution.
My page gets loaded with a sport_id parameter, and when the user specifies a sort-order I wanted it to submit a GET request for page.url/event?sport_id=1&sortby=viewers but it wouldn't preserve the sport_id parameter until I added a hidden field tag in the form like so:
<%= hidden_field_tag :sport_id, params[:sport_id] %>
This solution does submit an empty sport_id parameter if that parameter was not in the original request, but that is easily prevented by encapsulating the hidden field in an <% if params[:sport_id].present? %> condition.
Use hidden_field_tag.
Inside of your form, just set hidden_field_tags for the existing GET params, like so:
<% request.query_parameters.collect do |key, value| %>
<%= hidden_field_tag key, value %>
<% end %>
This will ensure that your existing params persist.
Rails 3?
<%= form_tag your_path(params.except(:controller, :action)), :method => :get do %>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
<% end %>