Rails remember form details without ActiveRecord / ActiveModel - ruby-on-rails

In my forms, users are displayed a specific view in a sequence depending on a value in a session. This is all working great except when users go back to a previous form, the form is not pre-filled with data from the session.
My model does not extend ActiveModel as I am not using local DB's for anything.
<%= form_tag do %>
<%= field_set_tag do %>
<label for="form_firstname">First name:</label>
<%= text_field "form", "firstname" %>
<label for="form_surname">Surname:</label>
<%= text_field "form", "surname" %>
<label for="form_dob_3i">Date of Birth:</label>
<%= date_select("form", "dob", :start_year => 1912, :end_year => 1994, :order => [:day, :month, :year], :prompt => { :day => 'Day', :month => 'Month', :year => 'Year' }) %>
# and many other fields including radio/check/text/select ...
# next / back buttons...
<% end %>
<% end %>
My question is how can I get the form to remember data from the session and pre-populate the field?
For text fields, I can add a value attribute like so, but I am not sure this is the best way to go about it:
<label for="form_address_line_1">Address:</label>
<%= text_field "form", "address_line_1", :value => session[:quote]['policyholder']['address_line_1'] %>
I am also not sure how to apply this to radio fields etc as the following would not work (although it makes sense to me):
<%= radio_button_tag 'form[homeowner]', 'yes', :checked => 'checked' if session[:quote]['policyholder']['homeowner'] = 'yes' %>
<label for="form_homeowner_yes">Yes</label>
<%= radio_button_tag 'form[homeowner]', 'no', :checked => 'checked' if session[:quote]['policyholder']['homeowner'] = 'no' %>
<label for="form_homeowner_no">no</label>

Use your back button in your view to render the previous form and not redirect to it...
so try this rather than redirect_to: render 'your_controller_action'

Related

selected values are resetting in page reload in ruby on rails

Iam new to ruby on rails.I want to show the selected values from drop down and checked radio button in page reload.
When the page is reloaded, the selected value of the drop down and radio button are getting reset.So please let me know how to fix this issue.Here is the code i'am using.
<%= form_tag :action => 'show' do%>
<strong>Select device: </strong> <%= collection_select(:device, :id, #devices, :id, :name, options ={:prompt => "-Select a device"}) %>
<br></br>
<strong>Chose:</strong><%= radio_button_tag :name,:time, false, :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph%>Graph
<% end %>
Try:
collection_select(:device, :id, #devices, :id, :name, {:selected => #your_selected_value, :prompt => "-Select a device"})
see radio_button_tag(name, value, checked = false, options = {})
<%= radio_button_tag :name,:time, #your_checked_value.eql?('time'), :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph, #your_checked_value.eql?('graph') %>Graph

rails multiple checkboxes in 1 db column. saves correctly but doesn't show as checked

Event saves the section id's in an array. But in the /events/1/edit view for a newly created event, the expected checkboxes are not checked. I'm guessing because the default for checkbox values is booleans.
Event.last.newsletters #=> ["108", "115", "116", "117", "118", ""]
I have a CRUD for Event. Each Event can belong to multiple Sections. I have this displayed as a collection of checkboxes with the simpleform gem.
<%= simple_form_for #event do |f| %>
...
<%= f.collection_check_boxes :newsletters, Section.all, :id, :name, :input_html => { :class => 'checkbox' } %>
This results in the following html:
<span><input id="event_newsletters_1" name="event[newsletters][]" type="checkbox" value="1" /><label class="collection_check_boxes" for="event_newsletters_1">Newsletter 1</label></span>
<span><input id="event_newsletters_2" name="event[newsletters][]" type="checkbox" value="2" /><label class="collection_check_boxes" for="event_newsletters_2">Newsletter 2</label></span>
etc. etc.
When I create a new event or edit an event, the newsletter values are saved properly in the model.
Try specify the param :checked:
<%= f.collection_check_boxes :newsletters,
Section.all,
:id,
:name,
:input_html => { :class => 'checkbox' },
:checked => #event.newsletters %>
Reference: how to preselect an association checkbox using simple_form
If you are using SimpleForm you can use this:
<%= f.input :newsletters, collection: Section.all, as: :check_boxes %>

Resetting the value of fields after submitting the ruby form

I have search field based on date entered.
The problem is whenever I am submitting the form then searched results comes good but search string disappears from the field as the page reloaded.
Can anyone tell me how to persist the search string in the field.
I am using ruby form and below is the code..
<%=form_for :messages, url: url_for( :controller => :time_sheets, :action => :late ), :html => { :id => "submitForm1" } do |f| %>
<div class="field">
<tr>
<td> From Date <%= f.text_field :leaveFrom, :id => 'datepicker2', :class => 'phcolor','data-validation' =>'required', :readonly => true %></td>
<td> To Date <%= f.text_field :leaveTo, :id => 'datepicker7', :class => 'phcolor', :readonly => true %></td>
<td >Late Time<%= f.text_field :on_duty, :id => 'timepicker5' %></td>
<div class="actions">
<td style="position: absolute;right: 178px;"><input type="button" class="btn btn-primary" id="submitid" tabindex="7" value="Go"></td>
</div>
</tr>
</div>
<% end %>
Your code form_for :messages suggests that there is an instance variable #messages, which holds the data of the fields in the form. Is there a valid #messages?
If you don't have Model for messages, I think of a simple workaround. You can add this to the action:
#messages = OpenStruct.new(params[:messages])
Or, just replace form_for with form_tag, and set value of each fields from params. form_for "creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.", which is not suitable here.
If your form isn't related to any model then use the form_tag helpers etc. Then you would do something like:
<%= text_field_tag :leaveFrom, params[:leaveFrom], :id => 'datepicker2', :class => 'phcolor','data-validation' =>'required', :readonly => true %>

Cant make form date_select hidden in rails

I am trying to create a form which loads upon a user clicking a date in a calendar, the form then is passed the date that is clicked through the URL and the controller assigns that date to the #date variable. I then create a date_select element and assign it the #date variable. This works fine but since I do not want the user to be able to edit the date in the form I want it to be hidden.
I pass these html options to the form but it doesn't seem to ever effect the HTML:
<%= f.date_select :date, :default => #date, :type => "hidden" %>
Am I missing something? I also tried passing it in an HTML hash :html => { :type = "hidden" } but that doesn't work either. Even when I try something different like :class => "something" it doesn't change the HTML. Is there something special about the date_select helper?
date_select accepts the options discard_day, discard_month and discard_year to do exactly what you are trying to achieve.
<%= f.date_select :date, { :discard_day => true, :discard_month => true, :discard_year => true } %>
Behind the scenes, it generates the following HTML code:
<input id="record_date_3i" name="record[date(3i)]" type="hidden" value="5" />
<input id="record_date_2i" name="record[date(2i)]" type="hidden" value="1" />
<input id="record_date_1i" name="record[date(1i)]" type="hidden" value="2012" />
No CSS tricks, no changes in your controllers.
Per the name, date_select generates <select> elements. In no version of (X)HTML does the select element support the type attribute. If you want a hidden form field then you should use the hidden_field helper, which generates <input type="hidden"> elements.
(To answer your implied question about using e.g. :class => 'something', the problem is that the options and html_arguments parameters must be two separate hashes, but if you do something like this:
<%= f.date_select :date, :default => #date, :class => 'something' %>
..the Ruby interpreter assumes that you have supplied a single hash, i.e. { :default => #date, :class => 'something' } (and really, can you blame it?), and since class isn't a valid key for the options hash it's ignored. Instead you have to make it obvious to Ruby that these are two separate parameters by doing something like this instead:
<%= f.date_select :date, :default => #date, { :class => 'something' } %>
<%# Hey Ruby, this is a different Hash! ----^ %>
See the difference? Of course you could go bonkers and be really obvious, e.g.:
<%= f.date_select(:date, { :default => #date }, { :class => 'something' }) %>
..but that's ugly and egregious so don't bother.)
You can put it inside a hidden div:
<div style="display: none;">
<%= f.date_select :date, :default => #date, :type => "hidden" %>
</div>
That will allow you to have all the fields and hidden you can also use for date and time select:
<div style="display: none;">
<%= f.datetime_select :date, :default => #date, :type => "hidden" %>
</div>

Rails - I need a checkbox to change a field in the DB

I know I have done this before, but for the life of me I can't figure it out.
I have a table with a "called" field in it. I need to use a checkbox to update the db and check if "called" is "true" or not. Doesn't need to be AJAX, just needs to update the field.
table: rsvp
field: called
Thanks a ton.
A simple approach without ajax could be using a checkbox inside a form and submitting the form with the checkbox javascript onclick event.
Example:
View:
<% form_for #rsvp, :id => "rsvp" do |f| %>
<%= f.check_box :called, :onclick => "$('#rsvp').submit()" %>
<% end %>
this if you are using JQuery... with prototype the onclick string will be:
$('rsvp').submit()
Controller:
#rsvp = Rsvp.find(params[:id])
if #rsvp.update_attributes(params[:rsvp])
# success
else
# fail
end
Reference:
check box
In view:
<% form_for :rsvp, :url => {:controller => "rsvp", :action => "update"} do |f| %>
<%= f.check_box :called %>
<%= f.submit "Update" %>
<% end %>
In rsvp controller, update method:
#RSVPobject.updateAttribute(:called, params[:rsvp][:called])
If you just want to do this just by clicking the checkbox, you need to go the Ajax road.
Try using an "observe_field" in your view.
<%= observe_field ":called",
:frequency => 0.25,
:update => 'feedback_to_user',
:url => {:action => :mark_as_called},
:with => 'called',
:on => 'click' %>
All the details here.
Don't forget to adjust your routes so that the "mark_as_called" action can be found.

Resources