Rendering js.erb Layout but html.erb view file - ruby-on-rails

When app gets javascript request it renders application.js.erb and index.js.erb by default, but i want to use index.html.erb and application.js.erb. How can i achieve that?
I want to use :
Application.js.erb + index.html.erb instead of
Application.js.erb + index.js.erb

add .html to end of partial name you want to render.
Example:
<%= render :partial=>'some_partial.html' %>
Instead of:
<%= render :partial=>'some_partial' %>

In your controller code you could
def index
#get records and whatnot
respond_to do |format|
format.html # index.html.erb
format.js { render :action=>:application }
end
end
Note: I don't know why you are doing this, but this will work. I recommend working within the rails standards.

Related

Is it possible to specify what js.erb file I want to run?

If I have the following code, Rails is setup to automatically look in the views folder and find photos/feed.js.erb. But what I want to do is to tell it to run users/feed.js.erb instead.
PhotosController
def feed
#title = "Favorites"
#user_feed_items = current_user.favorites.order('created_at desc').paginate(page: params[:page], per_page: 15)
respond_to do |format|
format.html {
render 'users/feed' }
format.js
end
end
Rails: Render a .js.erb from another controller?
format.js { render :file => "/users/feed.js.erb"}
render accepts the full path (relative to app/views) of the template to render. So, you can just enter users/feed
render "users/feed"
Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the :template option.
render template: "users/feed"
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

Render partial in rails controller called using ajax

I have rails application where I have part of views as partials and I re-factored couple of views as SPA. I have problem with one functionality. In one place I was rendering partial without calling controller like following:
def create
school_service.update_preview
#user = get_user
#result = prepare_result
render 'school/result/show', locals: {
pupil: #user,
result: #result
}
end
I was calling this method using form. Now I call this from JS using AJAX. Is it possible to render that view in the same way? I wouldn't like to rewrite 'school/result/show' to SPA. Thanks for all answers.
Your question says I was calling this method using form. Now I call this using AJAX. Is it possible to render that view in the same way?
Since you are using AJAX i assume you'll have something remote: true in your form something like:
<%= form_for(#user, remote: true) do |f| %>
# your fields
<% end %>
This will take you to the create action in your controller and you can have a respond_to block in your controller to handle your js format
def create
#user = get_user
#result = prepare_result
respond_to do |format|
format.js {}
end
end
This will allow you to have create.js.erb file in your views where you can write your js to render your partial
$(".your_parent_element_class").html("<%=j render partial: "school/result/show",locals:{pupil: #user, result: #result} %>")
For more details checkout working with javascript in rails

Rails AJAX - don't redirect or render

Using rails and .js.erb to make an AJAX request (and append values to a div), how do you prevent rendering a new layout? In other words, stay on the same page without going anywhere and just append the fresh data from the server in a div. No reloading the same page, no redirecting.
At the moment my controller looks like this
def update_shipping
#order = Order.find(params[:id])
#order.shipping_option_id = params[:shipping_options]
#order.save!
respond_to do |format|
format.js
format.html
end
end
and my form like zisss:
<%= form_tag update_shipping_order_path(#order), method: :put, remote: true do %>
<%= select_tag 'shipping_options', #options_for_select, onchange: 'this.form.submit()' %>
<% end %>
and my routes look like a so:
resources :orders do
member do
put :update_shipping
end
end
But I get a 'Template is Missing' error
Please help!!
You need to add a update_shipping.js.erb file under app/views/your_controller/ directory. Note the name of the javascript file should be same as the action. Since you have a remote:true in your form so rails will try to render a javascript template in your case update_shipping.js.erb.
Now in your update_shipping.js.erb file write some basic javascript to update the page elements like
#update_shipping.js.erb
$('.some-div').html(<%=j #model.some_value' %>)
Try this:-
respond_to do |format|
format.js { render :nothing => true }
format.html
end
If you don't want to render a layout, you can use !request.xhr? like so:
respond_to do |format|
format.html { layout: !request.xhr? }
format.js
end
If you're looking to get your ajax-powered JS to fire, you just need to call your .js.erb file the same as your view:
#app/views/controller/update_shipping.js.erb
alert("This JS is returned & fired after the Ajax request");
You'll be best doing this in your routes.rb too:
resources :orders do
put :update_shipping
end
A little late, I came across this searching for the same issue. It must of slipped out of my mind at some point while working with action cable, but what is needed is a http response code of no_content. Http response codes tell the browser how to act when a request is returned. Here is a link to a list of them, and their symbols in rails. More on 204 no content
Here is how it would look:
def update_shipping
#order = Order.find(params[:id])
#order.shipping_option_id = params[:shipping_options]
#order.save!
head :no_content #or head 204
end
edit: what solved the solution for me was a link provided by William Denniss in this stack overflow question

rails - Handling Layout content_for with AJAX

I have a an application layout template with:
<%= yield(:railed) %>
To handle contact in the right rail (right column 300px). In the actualy DEF SHOW page I use:
<%- content_for(:railed) do -%>
HTML STUFF goes here
<%- end -%>
The issue I'm having now is that for one of my controllers, Im using AJAX to hit DEF Show, and inject the content into the page. This work fine expect for it doesn't get the railed content as the layout template isn't being used in the format.js response.
So what's a smart way I can get the railed contet display with the AJAX request, and not have to write to separate pages for AJAX & Non-AJAX (direct URL).
Thoughts? I know some Rails genius has figured this out already :)
My controller:
def show
#thing = Thing.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.js
end
end
show.js.erb:
<%= render :partial =>"thing/show" %>
It's a bit of a hack but this is how I did it in rails 3.1 with haml and jquery:
show.html.haml:
- if request.xhr?
#content_for_sidebar.hidden
=#view_flow.get(:sidebar)
ajax_callbacks.js:
$("#nav a[data-remote]").live('ajax:complete', function(status, xhr) {
$($(this).attr('rel')).html(xhr.responseText);
$('#sidebar').html($("#content_for_sidebar").html());
$("#content_for_sidebar").remove();
});

How to return HTML directly from a Rails controller?

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

Resources