Resetting the value of fields after submitting the ruby form - ruby-on-rails

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 %>

Related

Can't get the drop down box value that is selected to show up in the next page

I am new to rails and very excited to learn all that I can. Everything is working fine except the drop down box. The box populate correctly and has all the value that I want. But when I click "Assign" and it goes to the next page, the value that I selected in the drop down is not showing up. I understand that I need to reference it in the link and I have tried putting different value in the link, but I have still not been able to crack the code.
<table class="listing" summary="Subject list">
<tr class="header">
<th><h3>Procedures</h3></th>
<th><h3>Notes</h3></th>
<th><h3>Assign To</h3></th>
</tr>
<% #procedures.each do |procedure| %>
<tr>
<td><%= procedure.procedure_name %></td>
<td><%= procedure.note %></td>
<td class="actions">
<%= collection_select(:post, :employee_id, #individuals, :id, :last_name) %>
<%= link_to("Assign", {:controller => 'projects', :action => 'new', :procedure_id => procedure.id}, :class => 'action new') %>
<%= link_to("Edit", '#', :class => 'action show') %>
<%= link_to("Delete", '#', :class => 'action edit') %>
<td>
</td>
</tr>
<% end %>
Thanks for the help
It's going to a new page because you're clicking on a link. What I'm guessing you want to happen is it send the information without reloading the page. That's accomplished with AJAX but Rails simplifies it to just remote: true
<%= link_to "Assign", {:controller => 'projects', :action => 'new', :procedure_id => procedure.id}, :class => 'action new', remote: true %>
Also, in the terminal type rake routes and replace the hash. I'm betting it's just new_projects_path(procedure)
<%= link_to "Assign", new_projects_path(procedure_id: procedure.id), :class => 'action new', remote: true %>
EDIT: What you want it a form. I'm just going to skip the erb but that's simple enough (but I'll put in the equals sign for you):
form_for procedure.projects.new, remote: true do |f|
= f.collection_select... #probably same as before, consult collection_select form helpers docs to be sure
= f.hidden_field :procedure_id, value: procedure.id
= f.submit
end
Like I mention below in the comments if projects#new is nested you should be able to skip the hidden field.

Is it possible to make a link that functions as same as form does?

Now I have 2 forms that submit a comment.
Form Type A
<%=form_for(([#community, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Form Type B
<%=form_for(([#user, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Now, I want to have link_to button that functions as same as those forms do if a user clicks it.
When the user click on the link, #comment will be automatically filled just like below.
From Form Type A
#comment = "this is for community"
From Form Type B
#comment = "this is for user"
How can I do that? As far as I understand my situation.
Form is put type, then link_to is get type so it's impossible to re-use transaction of form.
Not sure what you mean by "transaction of form" but if you're asking if you can create/modify data via a single button or link than the answer is Yes, it is possible.
You can actually put with a link_to in rails ({:method => :put} (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
If you want a button to do this you should checkout button_to (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to)
It's better to use button_to.

How to update my dropdown list without reloading the page

I've got a dropdown list where a user can select an option. The functionality works as it should but the page reloads each time. I've done a bunch of reading and seen examples but I'm having a hard time synthesizing the methods I've seen. I know I have to pass AJAX on a format.js call in the controller method like:
def update_device_position
...
if #cabinet.add_cabinet_item(#position_str,params[:position].to_i)
format.js
Then in the view, I have to add form_tag code like something like this:
<div id="add_device">
<%= form_tag(:url => {:action => url_for(:controller => "cabinets", :action => :update_device_position), :position => i , :id => #cabinet.id }, remote: true)%>
<td>
<%= select_tag :position_name, options_for_select(#selection_list) %>
</td>
<td>
<%= hidden_field_tag 'position', i %>
<%= submit_tag "Add" , :class => "btn" %>
</td>
</div>
When I select the new value for the cabinet slot and click submit, I get a routing error stating No route matches [POST] "/cabinets/99999999"
Thanks.

Rails remember form details without ActiveRecord / ActiveModel

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'

Why the controller does not "see" the value entered on the form

I've got this on a Rails 3.0.3 view:
<%= form_tag line_items_path(:product_id => #product), :remote => true do %>
<%= number_field_tag (:amount, 1, { :size => 3, :min => 1}) %>
<%= submit_tag t('button.add_to_cart'), :name => nil %>
<% end %>
The view renders quite okay. In the line_items_controller's create method I try to access the number field:
#cart = current_cart
product = Product.find(params[:product_id])
#line_item = #cart.add_product(product.id, params[:amount])
That did not work, so I tried to examine the number field by printing it to the console:
p params[:amount]
That printed "nil" no matter what I've entered on the form field. I also printed the whole params hash with
p params
and got
{"product_id"=>"1", "action"=>"create", "controller"=>"line_items", "locale"=>"fi"}
i.e. the amount field is not there, which explains the "nil", but I'm a bit (ok, a lot) confused here, as I've read the form_tag documentation, and gathered that the controller should see the field contents via params[:amount], yet it doesn't.
Please help me. What am I doing wrong?
/hp
I feel so stupid right now... The form works now, when it is compeletely inside a <td> tag, like this:
<tr>
<td colspan="2">
<%= form_tag line_items_path(:product_id => #product), :remote => true do %>
<%= number_field_tag(:amount, 1, :size => 3, :min => 1) %>
<%= submit_tag t('button.add_to_cart'), :name => nil %>
<% end %>
</td>
</tr>
I had the form_tag, and the corresponding end before the <tr> tag, and after the </tr> tag, respectively.
Thank you for your time!

Resources