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") %>');
Related
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
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!
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
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' }
One of my model objects has a 'text' column that contains the full HTML of a web page.
I'd like to write a controller action that simply returns this HTML directly from the controller rather than passing it through the .erb templates like the rest of the actions on the controller.
My first thought was to pull this action into a new controller and make a custom .erb template with an empty layout, and just <%= modelObject.htmlContent %> in the template - but I wondered if there were a better way to do this in Rails.
In your controller respond_to block, you can use:
render :text => #model_object.html_content
or:
render :inline => "<%= #model_object.html_content %>"
So, something like:
def show
#model_object = ModelObject.find(params[:id])
respond_to do |format|
format.html { render :text => #model_object.html_content }
end
end
In latest Rails (4.1.x), at least, this is much simpler than the accepted answer:
def show
render html: '<div>html goes here</div>'.html_safe
end
Its works for me
def show
#model_object = ModelObject.find(params[:id])
respond_to do |format|
format.html { render :inline => "<%== #model_object['html'] %>" }
end
end