Rails button_to for Custom Name / Value Submit - ruby-on-rails

In Rails is it possible to achieve an HTML output like this using button_to?
<button type="submit" value="1" name="id">Type 1</button>
<button type="submit" value="2" name="id">Type 2</button>
What are the reasons for using button_to over manually entering the HTML in my form?
...
EDIT:
Perhaps I should rephrase this? Seems the better way would be to add some hidden fields to the button_to form. It doesn't seem like I can do this.
So what is the correct Rails solution to pass extra hidden fields to a dynamic form generated via a button_to? Or should I just build a form manually?
e.g. I have:
button_to "Download", items_path(:release_id => release), :remote => true
and I want to pass an extra parameter in the form POST format => "download"

Seems my answer was to use the form_tag helper

Related

Ruby-on-Rails post/get parameters value is NULL

I am new to Ruby on Rails so I apologize if the answer is obvious.
Here is my routes
get 'user_location/show' => 'user_location#show'
Form in one of my pages
<form action="/user_location/show" method="post">
<input type="text" name="destination" />
<input type="submit" name="commit" value="Find city" />
</form>
Now when I try to print out my post variables on my webpage
I get null. Something like this
<b> The selected destination is </b>
<%params['destination']%>
What am I doing wrong? I suspect it has something to do with routes but I am not too sure. Any help would be appreciated.
Thanks
You're not printing params in your view.
There is typo:
<%params['destination']%>
should look like
<%= params['destination']%>
Another suggestion is to use Form Helpers for generating forms.
Since you are using POST method for form submission, so your HTTP Verb for user_location/show have to include POST also. So your route would be:
match 'user_location/show' => 'user_location#show', via: [:get, :post]
And in your view:
<%= params['destination'] %>

Successive form submission without losing data in Rails?

I am confused between the controller and views interaction. I have this initial form which validates the csv file uploaded. (POST form).
After this form is validated successfully, I give the user the option to confirm the details which, and this confirm button acts as another form.
The thing is I want to keep the details from the previous form values saved in the params hash. So basically I want to perform a merge with the second form.
Is this possible? If so, can you help me with the code for the second form cause currently it overrides the previous form. Both forms point to the same function in the controller.
<% unless #contents.blank? || #errors.present? %>
<form name="confirm_bulk_order" method="post" enctype="multipart/form-data" class="search line" action="/orders/create_bulk_order" id="confirm_bulk_order">
<div class="search-btn-align" id="confirmButton">
<input type="submit" name="confirm_bulk_order" value="Confirm Order" class="lmargin10 uiButton">
</div>
</form>
<% else %>
<form name="upload_bulk_order_csv" method="post" enctype="multipart/form-data" class="search line" action="/orders/create_bulk_order" id="upload_bulk_order_csv">
<div class="fileformField">
<span class="formlabel"> Upload CSV File: </span>
<input class="required" required="true" type="file" name="datafile"/>
</div>
<div class="search-btn-align" id="uploadButton">
<%= submit_tag 'Validate Bulk Order', :class => 'lmargin10 uiButton' %>
</div>
</form>
<% end %>
In controller orders
def create_bulk_corder
if #errors.blank? and params[:confirm_bulk_order]=="Confirm Order"
#Send the final REST order call
else
#contents = read_csv_file(params[:datafile]) if params[:datafile].present?
validate_order(#contents)
#Populate #errors etc, etc
....
....
end
render
end
What all changes must I make for this to be possible?
You're losing your params when the confirm button is pressed because they're not part of the confirm form. You could avoid this by adding a hidden field:
<form name="confirm_bulk_order" method="post" enctype="multipart/form-data" class="search line" action="/orders/create_bulk_order" id="confirm_bulk_order">
<!-- This assumes that #contents is a simple value... it's probably not, so you might need several hidden_field_tags here, one for each part of #contents that you want in your params -->
<%= hidden_field_tag "contents", #contents %>
<!-- You probably also want to show the user some info about their submission here -->
<div class="search-btn-align" id="confirmButton">
<input type="submit" name="confirm_bulk_order" value="Confirm Order" class="lmargin10 uiButton">
</div>
</form>
If you go with this approach, be sure to re-validate params[:datafile] after submission, since a malicious user could change this param value to get around your validation logic.
Also, I suggest factoring your action into two separate actions, and likewise for the views. You've got if-else blocks where you really ought to have separate files.
I suggest maybe using a gem that deals with multi paged forms or wizards: https://www.ruby-toolbox.com/categories/rails_wizards

Ruby on Rails form input

How does Ruby on Rails auto generate the forms input? I've come across the following code and have no idea how the HTML below is being rendered.
<%= f.input :first_name %>
Renders:
<div class="input string required"><label for="user_first_name" class="string required"><abbr title="required">*</abbr> First name</label><input type="text" value="Paul" size="30" required="required" name="user[first_name]" maxlength="255" id="user_first_name" class="string required"></div>
The HTML is generated by the Rails form helpers. Rails gives you a bunch of methods to make it easier to generate form markup so you don't have to worry about naming and typing out all of the attributes every time. Checkout that link to the docs to get more familiar.
In addition to what Chris said, the code f.input probably comes from either formtastic or simple_form. They're gems used to output a preset template using minimal code so you might want to check those.

I want to use bootstrap checkboxes

I want to use Bootstrap checkboxes.
<div class="btn-group" data-toggle="buttons-checkbox">
<button type="button" class="btn btn-primary">Left</button>
<button type="button" class="btn btn-primary">Middle</button>
<button type="button" class="btn btn-primary">Right</button>
</div>
How do I integrate it into a Rails application?
I would like to use them in a form. Specifically, how can I send and catch the selected data?
UPDATE:
I know how to use checkboxes in Rails. I don't understand how can I set their style via bootstrap.
Checkbox is - <input type="checkbox">Simple</input>
Bootstrap checkbox is <button type="button" class="btn btn-primary">Bootstrap</button>
UPDATE 2:
As example, see my question about radio buttons.
For example you can delete an item like this:
<p>Delete item <%= asset.check_box :_destroy %></p>
Or it's even easier if you use the simple_form gem, for example if you have a boolean attribute in your model checkboxes are the default:
<%= f.input :agreement %>
Or you can force checkboxes like this:
<%= f.foobar, :as => :check_boxes %>
Consider using bootstrap-rails to incorporate to bootstrap css and javascript libraries into you application. There are several flavors of rails bootstrap but I personally use this one because it is written in SASS (more commonly used with rails than LESS).
You have some issues with how you approaching this as its seems you have three options and only one of which I'd assume should be selected (for which I recommend using a select tag) and you are using buttons not checkboxes... but to integrate checkboxes into you application, rails indeed offers a methods, an example in a form_for is as follows:
- form_for #something_with_a_postion do |something|
= something.label 'Left'
= something.check_box 'position', 'left', {class: 'whatever_classes_for_bootstrap_needed'}
= something.label 'Middle'
= something.check_box 'position', 'middle', {class: 'whatever_classes_for_bootstrap_needed'}
= something.label 'Right'
= something.check_box 'position', 'right', {class: 'whatever_classes_for_bootstrap_needed'}
You can find more on form_for and check_box here.
I also refer you to this question. As well, here is a RailsCast pertaining to checkboxes.

Rails close without saving from edit page

I have a typical CRUD app, I would like to add a link next to the f.submit that allows you to go back to the index without saving changes. I thought it would be as simple as just making a link to the index, but it's saving changes anyway.
<%= button_to 'Close Without Saving', users_path %>
<%= f.submit %>
What's the rails way of handling this?
From the fine manual:
[button_to] Generates a form containing a single button that submits to the URL created by the set of options.
So using button_to inside a form will attempt to create a nested form but HTML forms don't nest. The result will be an HTML structure something like this:
<form>
<form><!-- from button_to -->
<input type="submit">
</form>
<input type="submit"><!-- from f.submit -->
</form>
But the browser will ignore the inner <form> and assume you meant this:
<form>
<input type="submit"><!-- from button_to -->
<input type="submit"><!-- from f.submit -->
</form>
Then pressing either <input type="submit"> will submit the outer form.
You can either create another submit button in the outer form and the controller can check which button was pressed or you can use link_to and style the link to match your submit button (or leave it styled like a link depending on your preference).
<button type="button" onClick="location.href='<%= users_path %>'">Close Without Saving</button>
<%= f.submit %>
This also worked... went back to basics and used good old fashioned HTML.

Resources