How to update my dropdown list without reloading the page - ruby-on-rails

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.

Related

Rails 3: Can't add correct route to legacy code

Believe you can help me.
I'm trying to add new functionality to legacy code (Typo). But it seems that there is some problem about routing.
In the project routes are generated the following way:
%w{advanced cache categories comments content profiles feedback general pages
resources sidebar textfilters themes trackbacks users settings tags redirects seo post_types }.each do |i|
match "/admin/#{i}", :to => "admin/#{i}#index", :format => false
match "/admin/#{i}(/:action(/:id))", :to => "admin/#{i}", :action => nil, :id => nil, :format => false
end
My functionality is about merging articles. For that I've added new action in the /admin/content controller:
def merge
#some code here
end
A piece of a view partial (_form.html.erb) added by me:
<% if current_user.admin? and !#article.id.nil?%>
<div class=''>
<h4><%= _("Merge Articles") %></h4>
<%= label_tag :merge_with, 'Article ID' %><%= text_field_tag :merge_with, nil, :size => 20 %>
<%= button_to 'Merge', admin_content_merge_path(:id => #article.id) %>
</div>
<%end%>
This partial is rendered by another partial (_edit.html.erb)
<%= form_tag(form_action, :id => "#{form_type}_form", :enctype => "multipart/form-data", :class => className) do %>
<%= render :partial => "form" %>
<% end %>
And finally _edit.html.erb is rendered by view new.html.erb
<%= render "admin/shared/edit", { :form_type => "article", :form_action => { :action => "new", :id => #article.id , :class => ('autosave')} } %>
The problem is how to write a correct route for the controller action above which will allow me to render an edit page containing newly merged article. I wrote:
match "/admin/content/merge/:id" => "admin/content#merge",:as => 'admin/content/merge'
rake routes output:
admin_content_merge /admin/content/merge/:id(.:format) {:controller=>"admin/content", :action=>"merge"}
But the new or edit action is being invoked as I can see.
Apparently, my route is wrong, isn't it?
Could you please help me with this.
Thanks in advance!
Update
Up-to-date new.html.erb:
<% #page_heading = _('New article') %>
<%= render "admin/shared/edit", { :form_type => "article", :form_action => { :action => "new", :id => #article.id , :class => ('autosave')} } %>
<% if current_user.admin? and !#article.id.nil?%>
<%= form_tag "/admin/content/merge/#{#article.id}" do %>
<h4><%= _("Merge Articles") %></h4>
<%= label_tag :merge_with, 'Article ID' %>:
<%= text_field_tag :merge_with %><br />
<%= submit_tag "Merge" %>
<% end %>
<% end %>
Read the hint from the course:
HINT:Nesting is invalid in HTML.
That means that you can't nest form tags, don't put the form tag in another form tag, your nested form wont be able to do a correct action.
Since you have to put your code at the end of the page, try and see how to do it with having your merging form tag below the main edit article form tag. So basically you can find where the big form tag ends and put it below it.
Try to see if you can figure it out, and if not, don't hesitate to ask :)
Btw. I think everybody had some problem with this

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

Rails 3 - button_to update same page with controller#method

In my project I have the following form_tag to select a Site
<%= form_tag({:controller => "hvacs", :action => "index"}, :method => "get") do %>
<div class="field">
<%= select :p, :site_sel, #user_sites.map{|s| [s.name, s.id]} %>
</div>
<div class="actions">
<%= submit_tag("Select site") %>
</div>
<% end %>
This form_tag updates the index page through calling its method in the controller again.
I have the following button_to
<td><%= button_to 'Select', {:controller => "hvacs", :action => "select"}, :method => "get" %></td>
I would like to achieve a similar update with this as above rather than redirect to a new page with "select_path" etc, but the above does not seem to work.
How can I achieve this? Cheers!
OK, this looked so much like my AJAX problem, I tried to make it one!
I think all you need is a simple render statement in your select action
render :index
or
render :action => 'index'
But see http://guides.rubyonrails.org/layouts_and_rendering.html#using-redirect_to for more.
The following solution worked. Apologies if I was not so clear on what I was looking for.
<%= button_to 'Select', review_hvacs_path(:h => hvac, :a => params[:a], :s => params[:s]) %>
I was trying to pass parameters with the button, while staying on the review page.

Ajax form not showing up with ruby on rails form_remote_tag

This is all new territory for me, but I am working through a Rails book that was written before start_form_tag was deprecated and I am running into problems with the books example code using remote_form_tag. I have the other forms working but can't get this one up and running. Here's the code:
<h1>Categories</h1>
<ul id="category_list">
<%= render :partial => 'category', :collection => #categories %>
</ul>
<br/>
<p id="add_link"><%= link_to_function("Add a category", "Element.remove('add_link');
Element.show('add_category')") %></p>
<div id="add_category" style="display:none;">
<%= form_remote_tag(:url => {:action => 'new'},
:update => "category_list",
:position => :bottom,
:html => {:id => 'category_form'}) %>
Name: <%= text_field "category", "name" %>
<%= submit_tag 'Add' %>
<%= end_form_tag %>
</div>
This is exactly how it appears in the book, and doesn't compile. I've tried changing to match the form_tag blocks but the form enclosed by the "add_category" div never shows up.
Thanks!
UPDATE: Just found that it doesn't appear that the prototype script is not getting loaded.
both the remove and show methods on Element are showing up as undefined in Firebug. I am not sure why it is not showing up though.
I left out the = sign when including the javascript libraries in standard.html
Line should be this:
<%= javascript_include_tag :defaults %>
Vote to close this question?

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