Why is application.html.erb being called on a js response? - ruby-on-rails

In my Rails app I have a Ajax call to update a property:
def add_properties
#conversation = Conversation.update(params[:conversationId], :points=> => params[:conversation][:points])
respond_to do |format|
format.js { render :partial => "add_properties" }
end
end
And my _add_properties.js.erb is simple:
$('#ajaxFeedback').html('Property updated').show().fadeOut(4000);
What I am finding is that the call to the partial is choking due to some JQuery UI declarations in my application.html.erb. When I remove the declarations, all works well. How can I simply render the partial without calling the application.html.erb?
Any help would be appreciated!

Have you tried?
format.js { render :layout => false, :partial => "add_properties" }

Why not just rename that to add_properties.js.erb
call
respond_to do |format|
format.js
end

Related

How to render a js.erb with the same name as a member action in Rails?

I'd think this would go automatically, as I have already managed to do so for my destroy action, but no JS in activated.
My controller action looks like this:
def visability_toggle
#picture = Picture.find(params[:id])
#picture.toggle! :visable
respond_to do |format|
format.js { render :action => 'visability_toggle' }
end
end
And for now my pictures/_visability_toggle.js.erb that I would like to match my controller looks like this:
alert('Picture id: #{#picture.id}');
The action itself works. Also this JS is correct and worked when I added it with render js: ... in the controller action.
As mentioned: I should have left the underscore out when naming my template.
Just try this..
respond_to do |format|
format.js { :location => path_to_controller_method_url(argument) }
end
How do you respond_to another js file in the controller using Ruby on Rails?
respond_to do |format|
format.js {
:template => "pictures/visability_toggle.js.erb",
:layout => false
}
end

How do you respond_to another js file in the controller using Ruby on Rails?

I basically have an action that because of logic needs to return with the contents of another js file. How do I go about doing this? Thanks
app/controllers/classrooms_controller.rb
def create
if params[:test_logic]
respond_to do |format|
format.js { render 'create_differently' } # This doesn't work.
end
else
redirect_to root_path
end
end
app/views/classrooms/create_differently.js.erb
alert('hi')
You need to add
:layout => false
to avoid the rendering of the html layout for your js file.
Additionally you could define the different js-file like this
:template => "classrooms/create_differently.js.erb"
both together:
format.js {
render :template => "classrooms/create_differently.js.erb",
:layout => false
}
For browser-based testing, please be aware calling js not html!

rails ajax pagination with partials

I am using the will paginate gem for pagination. Things wok fine for me.I am trying to ajaxify it and I followed the http://railscasts.com/episodes/240-search-sort-paginate-with-ajax tutorial. But Ajax request is not being fired for me.
I have a index view. The view renders a partial called "_browser_form" which in turn renders a partial called "_listing". So I wanted to paginate the table in the _listing.
Please let me know if there is any error in my approach.
My controller:
def index
#ics = Ic.search(params[:root_name],params[:suite_name],params[:case_name],params[:name],'f').paginate(:per_page =>5, :page => params[:all_ics])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #ics }
end
end
My _browser_form.html.haml which is rendered from index.html.haml
- form_tag "/ics/mass_action", :method => :post, :multipart => true do
<div id="update_ics_table">
= render "listing", :show_check_boxes => show_check_boxes, :root_name=>params[:root_name],:suite_name=>params[:suite_name],:case_name=>params[:case_name],:name=>params[:name],:ic_filter=>1
</div>
=will_paginate #ics,:param_name=>:all_ics
My index.js.erb file:
$('#update_ics_table').html("<%= escape_javascript(render :partial => 'listing' ,:object => #ics) %>")
My .js file:
$(function () {
$('#update_ics_table .pagination a').live('click',
function () {
$.getScript(this.href);
return false;
}
);
});
Thanks,
Ramya.
Remove what you're doing in the Javascript and do this in the controller:
format.js {
render :update do |page|
page.replace 'listing', :partial => 'listing'
end
}
Similar: Best way to get will_paginate working with Ajax
It might have something to do with the fact that you're using HAML views and an js.erb file, but I don't use HAML so I can't be certain. Have a look at nex3's answer at this SO: How to send back js.haml in rails
If that is not the case, I think you need to change your respond_to controller action block to this:
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #ics }
format.js #<------ This will execute index.js.erb
end

my .js.erb file is not being called

In my rails application I am trying to access pagination via an ajax call. I am triggering a script via index.js.erb file. The control doesn't go to that file. Please help
My controller:
def index
ics_per_page=params[:ics_per_page]||5
ics_per_page=ics_per_page.to_i
#ics = Ic.search(params[:root_name],params[:suite_name],params[:case_name],params[:name],'f').paginate(:per_page =>ics_per_page, :page => params[:all_ics])
respond_to do |format|
format.js
format.html # index.html.erb
format.xml { render :xml => #ics }
end
end
My index.js.erb file:
console.log("inside index.js.erb");
$('#listing').html('<%= escape_javascript(render("listing")) %>');
Thanks,
Ramya.
Could you check again. I think the file index.js.erb is rendered but the content is not executed because of a wrong content type. set the content type to specify that it is javascript.
format.js { render :content_type => 'text/javascript' }

Is there an AJAX function that can just GET a rails partial?

My controller is shared by links from a search result page that needs a layout, and from the profile page itself that does not need a layout. What I would like to accomplish is a single controller method show that is both capable of drawing the partial by AJAX from the profile page, and draw the partial and layout from the search results.
The most important restriction I have is that I can not set the dataType: to script . It has to be html . So I can't use that spiffy AJAX script call that renders the JS without the controller's format.html getting involved.
tabs_controller.js
def show
#organization = Organization.find(params[:organization_id])
#tab = #organization.tabs.find(params[:id])
respond_to do |format|
format.html { render :partial => 'tab', :layout => 'myHQpage' }
format.js
end
end
javascript
$.get($(this).attr('href'), null, null, "html");
show.html.haml
= render :partial => 'tab'
What AJAX function can I use here that just draws the partial, and not the entire layout?
I'm not sure to really understand but this may help you.
Yo can add a :layout => false to the format.js block.
def show
#organization = Organization.find(params[:organization_id])
#tab = #organization.tabs.find(params[:id])
respond_to do |format|
format.html { render :partial => 'tab', :layout => 'myHQpage' }
format.js { render :partial => 'tab', :layout => false }
end
end
Keep the 'script' dataType setting and just add a wrappertag around your search results. Then use the jQuery replaceWith function to replace the content of that tag:
_tag.html.erb:
<div id="search">
...
</div>
show.js.erb:
$('#search').replaceWith('<%= escape_javascript(render "tab") %>');

Resources