Reset fields after submitting a ruby โ€‹โ€‹form

I have a search box based on the entered date.

The problem is that when I submit the form, the search results go well, but the search bar disappears from the field when the page reloads.

Can someone tell me how to save the search string in a field.

I am using the 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 %> 
+7
source share
2 answers

The code for form_for :messages indicates that there is an @messages instance variable that contains the field data in the form. Is there a valid @messages ?

If you do not have a model for messages, I am thinking of a simple workaround. You can add this to the action:

 @messages = OpenStruct.new(params[:messages]) 

Or simply replace form_for with form_tag and set the value of each field from the parameters. form_for "creates a form and an area around a specific model object, which is used as a base for polling about field values.", which is not suitable here.

+1
source

If your form is not associated with any model, use form_tag , etc. Then you will do something like:

 <%= text_field_tag :leaveFrom, params[:leaveFrom], :id => 'datepicker2', :class => 'phcolor','data-validation' =>'required', :readonly => true %> 
+1
source

All Articles