I have a single form partial:
<% form_remote_for(#snapshot_comment, :html => { :class => "snapshot_comment", :id => "snapshot_comment" }, :auth_key => params[:auth_key]) do |f| %>
some stuff...
<span class="button"><%= f.submit 'Submit ยป' %></span>
or cancel
<% end %>
It gets input into tooltips into a page multiple times via jQuery (targeting snapshot_id). This works well:
//Insert class and partial by targeting snapshot id
$j('<div class="info"><%= escape_javascript(render :partial => "snapshot_comments",
:locals => {
:snapshot => #snapshot,
:comments => #comments
})
%></div>').appendTo("#slide_<%= #snapshot.id %>");
The partial (snapshot_comments) contains the code for the form:
<%= render_snapshot_comment_form_for snapshot %>
The problem is that when data gets posted - it posts as many times as there are forms on the page.
So If a user enters info and clicks submit --- the content is inserted in every tooltip rather than just the tooltip it belongs to. I am using Qtip2.
Is there a way to submit the form specific to the tooltip it resides in?
If the form is being rendered multiple times per page from the same partial, then it sounds like your element IDs are the same from form to form within that page.
If your elements don't have unique IDs, then the response gets put into each place where that ID appears.
For example - if you have a page that puts results into a div with an id of "output", and that div gets rendered from a partial 5 times (each time with the same id of "output"), then you're going to see the response rendered 5 times.
Do a "view source" on your resulting page, and look for duplicate IDs, and see if that's the case.
Related
I am working on a rails web app which manages students and courses. I have a courses controller which has the following index action:
def index
#courses = Course.paginate(:page => params[:page], :per_page => 1)
#courses.order(:startDate)
##thisCourse = Course.find(params[:page])
end
So pretty standard except for one thing - all the details of a single course are shown on one page and to show the details of the next course, you move to the next page of the pagination.
The problem is that in this index page showing the details of 1 course per pagination, I have a "Sign Up!" button which when pressed needs to create a a new record in the 'signups' db table which has the automated 'id' field and then the 'user_id' and the 'course_id' fields.
The 'user_id' is easy to find (current_user.id).
The 'course_id' is proving difficult. I imagine that pressing the Signup button should send the course_id to the signups_controller where a create function can do the work. But how can I get this exact course ID from the index page to the signups_controller's create action?
As you can see in the code I pasted from the courses_controller's index action,the '#thisCourse' variable has been commented out because I have found no way to define which course is currently being shown on the page.
The fields are rendered by the will_paginate Gem so I'm not sure how it's generating the fields but I was thinking that maybe I could create a named hidden field which includes the course_id and use that when the sign up button is pressed, however I'm not sure how to go about it.
Does anybody have any ideas?
Thanks!
Well, you can use show method (output one course) instead of index(output all courses) method, that will always get your course id through params.
Basically I changed my approach to the problem. I removed the button which was supposed to call the signups_controller and create the new record in the signups table. This button was replaced by adjusting the form_for helper so that it's submit button would send all the necessary data to the signups_controller (including the id value which was added to the form as a hidden field).
The form ended up looking like this:
<%= form_for course, :url => {:controller => "signups", :action => "create"}, :method => "post" do |f| %>
<%= hidden_field_tag :course_id, course.id %>
<%= f.label :"Course Title" %>
<%= f.text_field :courseTitle, class: 'form-control' %>
+ all fields included in the form....
<%= f.submit "Sign Up!", class: "btn btn-primary" %>
<% end %>
This parameter of form_for defines which controller and which action in that controller is the submission target:
:url => {:controller => "signups", :action => "create"}
and this parameter overwrites the default http action (default is PATCH but in this case I wanted to POST i.e. create a new entry in the signups table):
:method => "post"
I'm not sure if this is a very quick and dirty solution but technically it gets the necessary data to the correct destination controller.
I'm making a Debt management app and I have #opened_debts and #all_debts (opened and returned debts) in my controller.
By default I load only #opened_debts on page. But also I have a link for showing all debts on a page.
Show all debts
I want page to reload debts_div with #all_debts after clicking this link.
If I make a <script> tag with click event in the bottom of a page to load it with
$('#debts_div).replaceWith('<%= j render #all_debts %>');
Rails in fact renders all debts and make them invisible for user until he click the link. But it makes page loading slower with all this huge script.
Can I load this list without rendering it on a page until user clicks link?
Thanks in advance!
use rails_ujs
<%= link_to "Show all depts", model_path(), :remote => true %>
then your request will be sent as ajax. create a view for js with
_index.html
<div>some code</div>
inside index.html.erb to make DRY
<%= render :partial => 'index.html' %>
index.js.erb to make DRY
$('#debts_div).replaceWith('<%= j( render :partial => 'index', :format => :html ) %>');
that should work
In my Rails (3.1) app, I have a shopping cart where I show the sales tax field and total on a page after the person enters their State.
It works correctly, except when there is an error and the person has already entered their State. In that case, the field value doesn't change (since the State field is populated already), so the jQuery (update_sales_tax) isn't triggered again.
Is there a way to render the view again (in case of errors) so the person doesn't need to fill out the entire form again, but have the sales tax calculated still? Or maybe a better approach to showing the user their sales tax on the page dynamically (since it's dependent on the State)?
In other words, my controller does this format.html {render :action => 'new'} if there is an error in Orders#new. It populates the fields in the form that the person already entered, however it doesn't recalculate the sales tax which is triggered onChange of the :state field via jQuery. How do I get this to recalculate if the view renders again and :state is already populated?
In the Orders#new view I have:
<%= form_for(#order, :method => :post) do |f| %>
<%= f.hidden_field :total_sales_tax, :value => #tax_amount %>
<%= f.fields_for :products_shipping do |shipping | %>
<%= shipping.select(:state , Order::STATES ,{:prompt => 'Select State'}, :id => "ship_state",:onchange => "update_sales_tax()") %>
<% end %>
<% end %>
<script>
function update_sales_tax(){
jQuery.ajax({
url: '/orders/order_update',
dataType: 'script',
type:'get',
data:'stateTo='+jQuery("#ship_state").val()
})
}
</script>
In orders/order_update.js.erb:
jQuery('#cart_details').html('<%= escape_javascript(render(:partial => #cart)) %>');
jQuery('#order_amount').val('<%= format("%.2f", #grand_total) %>');
jQuery('#order_total_shipping').val('<%= format("%.2f", #cart.total_shipping) %>');
jQuery('#order_total_sales_tax').val('<%= format("%.2f", #tax_amount) %>');
I figured it out, I just needed to add this line above the function:
window.onload = update_sales_tax();
I have a set of 3 collection_selects that I would like to filter the next select. Upon selecting an item in the first select box, it should limit the possible values of the second and third select boxes.
A similar data model would be as follows...
Tables, and fields
Lock
lock_id
brand_id
master_id
regular_id
Lock Brand
brand_id
Master Key
master_id
brand_id
Regular Key
regular_id
brand_id
So while filling in the form for adding a new Lock there would be select boxes for brand, master, and regular. Upon selecting a brand, it would then limit the master and regular keys to those that are only of the selected brand.
I've found a couple partial examples of how to do this, however none are complete. I'd appreciate a complete example of how to implement this.
All you need to do is bind the selects with an event listener that fires off an ajax request populating the other 2 partials containing the subsequent selects. i.e.
<%= form_for #thing, :remote => true do, :update => 'second_form' |f| %>
<%= hidden_field_tag 'form_partial', :value => 'form_2' %>
<%= select_tag :some_param, [[a,a],[b,b],[c,c]] :class => 'submittable' %>
<%= f.label :checked %>
<%= thing.name %>
<% end %>
<div id='second_form'>
</div>
<div id='third_form'>
</div>
class ThingController < ApplicationController
def update
checkboxes = some_logic_to_select_checkboxes from params[:some_params]
render :partial => params[:form_partial], :locals => {:select_menu => select_menu}
end
end
$('.submittable').live('change', function() {
$(this).parents('form:first').submit();
return false;
});
Your form partial should contain your forms populated with whatever selects you want enabled. Thought is was redundant to just repeat the form.
Is it possible to set the location you want to submit a form to dynamically at runtime with a form_tag? What I have is a shared partial view that is used to filter data on several screens. The view contains several different form fields and a submit button, so the UI for these controls is the same for all the screens that use it, thus the shared partial view. The difference is that I want the submit to go back to a different location depending upon which page the partial view is contained in. Is there someway to pass the destination in via the render tag like the following?
<%= render 'shared/event_filter', :dest => event_path %>
and then consume it within the partial view as follows?
<%= form_tag(:dest, :method => "get") do %>
The above code doesn't work, it gives me a nomethod error on the :dest in the form_tag, but I was just putting in this question to help illustrate what I was trying to do.
Any help/suggestions would be appreciated.
I think you might be looking for something along these lines:
<%= render :partial => 'shared/event_filter', :locals => {:form_action => event_path} %>
Which just renders the partial named shared/_event_filter.html.erb and passes in a variable called form_action with value of event_path.
Then inside your partial:
<%= form_tag form_action, :method => "get" do %>
<!-- snip -->
<% end %>