RoR... my scaffold table doesn't fill when i use radio buttons - ruby-on-rails

I have a problem, i made a scaffold, generating the table "requirements", i want the user to fill the fields of the table in the edit and the new requirement with select boxes and radio buttons. The select box and the radio buttons appear in the explorer but when i select one option or one button, that value selected it's not reflected in the db. The code im using its the next: (As you can see i used the original cicle f.label(:notif_card) and f.text_field(:notif_card) generated by the scaffold, but i deleted the last one and used the select box in this case.)
<%= f.label :notif_card %><br />
<% value = { "Good" => 0, "In Progress" => 1, "Denied" => 2 }%>
<%= select( #requirement, :notif_card , value) %>
<% if value == 1 %>
<% #requirement.notif_card = 1 %>
<%end%>
I just want to delete that text_field and replace it with a select box! Everything you can do i will appreciate it alot! If something needs to be in the model or in the controller besides the code that i'm using please let me know. Thanks for your help!

if you want to use the radio buttons for storing values from form to DB you can use the radio buttons..
the syntax will be like
<b>notif_card</b><br/>
<%= f.radio_button:notif_card,'0'%>Good<br\>
<%= f.radio_button:notif_card,'1'%>In progress<br\>
<%= f.radio_button:notif_card,'2'%>Denied<br\>
<%= f.submit %>
this will store values on the database by using radio buttons.
if you want to use check box method for the same, you can use but it is of Boolean method.so needs to be declared as boolean when start generating scaffolding it.

Related

Rails select 'Other' option creating text field

Given a standard select code:
<%= f.select :type_name, [['Genomics','Genomics'],['Proteomics','Proteomics'],['Transcriptomics','Transcriptomics'],['Other','Other'] %>
Can someone explain how I would go about creating a text field when 'Other' is selected? So that the type_name can be something other than the options in the select?
I realise this is a simple question, but I haven't found a concise answer as of yet!
There are lots of ways to do this, but they all require JavaScript. The general approach I like is to put a hidden text field in the form, then attach a JavaScript event handler to the select tag that shows the field when the "Other" option is selected.
Here's a gist of the script I typically use for this. It handles the JavaScript binding using data attributes. Add the script to your assets, then put something like this in your form:
<%= f.select :type_name, [['Genomics','Genomics'],['Proteomics','Proteomics'],['Transcriptomics','Transcriptomics'],['Other','Other'] %>
<%= f.text_field :type_name_other, "data-depends-on" => "#object_type_name", "data-depends-on-value" => "Other" %>
where #object_type_name is the HTML id of your dropdown.
You need to create an attr_accessor on the model f is attached to (like type_name_other), add a text_field to the form below the select for type_name_other in a div that is initially hidden (in CSS: display:none), then create a javascript listener that detects when the select form has changed and if the selected ansser is "other" show the hidden field else hide it. You will then need to see if type_name_other has a value when processing the form and use it if so.

How to read values from multiple checkbox in ruby-on-rails

How to read values from checkbox in rails.
Suppose I have brands and products.Under brands table, I have 3 brands say nike,reebok and puma. When none of the check box is selected It should display all the products say shirts,trousers,skirts. If nike brand is selected then should display onlt nike related products.Also user should have access to select more than 1 brands and we should able to display corresponding product details.
Conceptually what you are doing is a lot like creating a form that acts as a search engine (Ryan Bates does great screen casts and can help get you if you get lost http://railscasts.com/episodes/37-simple-search-form). Instead of entering text, the form uses checkboxes to create an array of brands the user wants to see.
First Create the form
<%= form_tag your_path_here_path, method: :get do %>
<%= label_tag :Nike %>
<%= check_box_tag 'brands[]', '1' #assuming 1 is the id of the Nike brand%>
<%= label_tag :Reebok %>
<%= check_box_tag 'brands[]', '2'%>
<%= label_tag :Puma %>
<%= check_box_tag 'brands[]', '3'%>
<%= submit_tag 'Get Products'%>
<%- end %>
Then in the controller processing this request search for the products that match brands in the form like this
Products.where('brand_id IN (?)', params[:brands])
To go a bit further. People use JS for these types of problems so that as the user checks different brands the products will automatically reload on the page without the user having to hit a submit button.
This could be accomplished by writing a JQuery function that listens to check events on your brands checkboxes and then
1) makes an Ajax call in the background to get all the relevant
products
2) Removes all the products you don't currently need on the page
3) Shows all the new products.
If you don't know any JQuery this project could be an interesting way to get started learning. Again, railscasts can help http://railscasts.com/episodes/136-jquery

Ruby On Rails: make radio button default selected first time

I am new to RoR. I want to know how do we make a radio button default selected just first time while creating new record?
I tried
<%= radio_button_tag 'scheduler', 'now', true%> Now <br>
<%= radio_button_tag 'scheduler', 'future'%> Future<br>
It makes the first radio button (having value Now) checked but when i select second radio button (Future) and leave other mandatory fields blank on the form then again the first radio become selected instead of second.
How to resolve this.
Note: the field "scheduler" is just a virtual field on this form that will display another HTML containing a datetimepicker if "Future" option is selected and that datetimepicker field "schedule_date" is the actual field of the model. If option "Now" is posted then simply it will save the current date and time into the database table for that field "schedule_date". So i just have to show/hide an HTML by using these radio buttons.
When you say 'leave the mandatory fields blank' i guess you are submitting the form and getting the edit form back from the server with a warning flash? What I think you need to do here is instead of setting the Now field to true, you need to use a variable and set it to true on when creating a new form. Then when you submit the form to the server the controller's create method should look at the params hash, set the variable, and if the submission fails validation is should return the edit form with the Now variable set to false and the Future variable set to true
so in the create method of the controller you have something like
#now = params[:scheduler][:now]
in the new method of the controller you have:
#now = true
and in the view:
<%= radio_button_tag 'scheduler', 'now', #now%> Now <br>
<%= radio_button_tag 'scheduler', 'future', !#now %> Future<br>
This is true because you have set the first radio button to be selected by default. So when you select the second radio button and then refresh/submit the form, the page comes to its original position losing the previous selected second button.
This is just like when you enter the form fields and then refresh the form or come back again to the form after going back, you loss the data that you filled previously.
Solution:
I say get the checked radio button value and set it using jquery as-
$('input[name="myradios"]').change(function() {
alert($(this).val());
});
Try:
<%= radio_button_tag 'scheduler', 'now', true %> Now <br>
<%= radio_button_tag 'scheduler', 'future', params[:scheduler].eql?("future") %> Future<br>

How can I use radio buttons properly in Rails

I am doing my first project using Ruby on Rails and need to display a set of radio buttons. It should behave exactly like a selection list. For usability reasons, I need it in a radio buttons format.
In my project, I use the collection select which also allows me to display on the edit page as follows:
select('project','project_type_id',#project_types.collect{|project_type|[project_type.name,project_type.id]}) <br>
I need something exactly like this, (especially the ability to display the selected value in the edit page) but using radio buttons.
I did a Google search and read the entire Rails guides on radio buttons but I can't find the answer.
How can I do this?
I suppose you can do it like this in your view
<% #project_types.each do |project_type| %>
<%= radio_button("project", "project_type", project_type.name) %> #assuming you have a name attribute on project_type
<% end %>
If you want a particular radio button to be checked then you can pass the checked option like so
<%= radio_button("project", "project_type", project_type.name, {:checked => true}) %>

how to use variable name with image tag

my controller uses code like this:
if params[:commit] == "Submit"
this used to work fine when I just had buttons. however, now I am using images as buttons like below:
<%= image_submit_tag 'butons/Add-08.png', :class => 'image-button-submit' %>
How can I pass the commit variable with value Submit along with this image_submit_tag?
KandadaBoggu is right, but that will (in most browsers) give you a params['commit.x'] and params['commit.y'] rather than just params['commit'].
If there's only the one button on the form (and you're posting to the same action that renders the form) you can do if request.post? instead, but that's only going to work if there's just one button, ie: only submit, not submit and cancel.
As per the image_submit_tag documentation you can pass any HTML options. I haven't tested this but the following code should work.
<%= image_submit_tag 'butons/Add-08.png', :name =>"commit", :value =>"Submit" %>
I would try passing it as a hidden field inside of the form you're submitting.
hidden_field_tag "commit", "Submit"

Resources