Turn off layout for one of action - ruby-on-rails

My situation:
View action of ReportsController should render pure html, but not as a file (to view it in browser and save it after). So for rendering I use view template view.html.erb and i neet to turn off any layouts for this action. But in other actions of this controller layouts should stay untouched.
Works only turning off for whole controller like this:
ReportsController < ApplicationController
layout false
But that doing it wrong :( for all the actions
I tried to use something like this in action:
def view
#report = Report.new(params[:report])
unless #report.valid?
render :action => 'new' and return
else
render :layout => false
end
end
What should I do?

This should do it,
def view
...
render :layout => false
end
Link to Documentation

Try this:
ReportsController < ApplicationController
layout false
layout 'application', :except => :view

In the respond block, add layout: false.
For example:
respond_to do |format|
format.html { render :layout => false }
end

If you want to get a non-standard template, with no layout you can use:
def non_rest
render template: 'layouts/something_new', layout: false
end

Related

ruby on rails how to render a page without layout and other head field

else
respond_to do |format|
format.html { render "tabelle/show" }
end
end
I want to render the page ...with only the code in that page....not add <head>...layout and <body> field in ruby on rails.
I only want to show the result of code in the page tabelle/show.html.haml
You can do it like this:
format.html { render "tabelle/show", :layout => false }
Controller:
layout false, only: [:method_name]
this is very useful when you using render_to_string
add
:layout => false
Example:
render "tabelle/show", :layout => false
If you do not want to specify the view to use.
Rails is smart enough to know which view template to use based on the Controller Action you're on.
For example, if you're on the show action of the TabellesController you wouldn't need to specify render "tabelle/show" in your Controller Action because Rails will already assume that and will automatically try to render the file in app/views/tabelles/show.html.erb.
So if you're sticking with all of those defaults then you can just use the following to render without the typical layout template:
def show
# Other stuff in your Controller Action.
render layout: false
end
This will render app/views/tabelles/show.html.erb but without the layout template automatically.
Noice.

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!

ROR 3 - Specifying specific view file, ignoring application layout

I would like to specify a specific view file to render instead of the default one corresponding the REST architecture, meaning out of my 'create' function in the controller I would like to invoke the 'new' view file - which I believe can be done using:
def create
.
.
render :new
end
But I also need that view file to ignore the cross-site layout specified in layouts/application.html.erb? is there a way to do that?
If it was out of the 'new' function, I could just state "render :layout => false" .. but I need it out of the 'create'
is there something like:
render :new, layout => false
Thanks!
Another way is this:
render :template => :new, :layout => false
I' not sure about that, would have to try it, but i think that you can do this :
layout 'application', :except => :action_name
to exclude the action in your controller.
EDIT : I just tried it, it works indeed :)
You can do what you mentioned
def create
render :new, :layout => false
end
You can then add the conditions like this
def create
render :new, :layout => user_signed_in?
end
or the other way around depending on your need

How do I specify ":layout => false" in Rails' respond_with?

I have this setup:
class UsersController < InheritedResources::Base
respond_to :html, :js, :xml, :json
def index
#users = User.all
respond_with(#users)
end
end
Now I am trying to make it so, if params[:format] =~ /(js|json)/, render :layout => false, :text => #users.to_json. How do I do that with respond_with or respond_to and inherited_resources?
Something like:
def index
#users = User.all
respond_with #users do |format|
format.json { render :layout => false, :text => #users.to_json }
end
end
Assuming you need JSON for an Ajax request
class UsersController < InheritedResources::Base
respond_to :html, :js, :xml, :json
def index
#users = User.all
respond_with(#users, :layout => !request.xhr? )
end
end
This seems like the cleanest solution to me.
Or to prevent you having to hardcode responses for each format in each action.
If you have no layouts for any of the actions in this controller it would be nicer to do:
class UsersController < InheritedResources::Base
respond_to :html, :js, :xml, :json
layout false
def index
#users = User.all
respond_with(#users)
end
end
I love #anthony's solution, but didn't work for me... I had to do:
respond_with(#users) do |format|
format.html { render :layout => !request.xhr? }
end
ps: posting an "answer" instead of a comment because stackoverflow comment formatting and "return key == submit" is infuriating!
I just found this out:
Even if it's JSON, Rails is still looking for a layout. As such, the only layout that it finds, in our case, is application.html.
Solution: Make a JSON layout.
So for instance, if you put an empty application.json.erb with a single = yield inside, next to your HTML one, the HTML layout is bettered by the JSON one. You can even use this to surround your JSON with metadata or things like that.
<%# app/views/layouts/application.json.erb %>
<%= yield %>
No other parameters needed, it automagically works!
Tested in Rails 4 only
class UsersController < InheritedResources::Base
layout -> (controller) { controller.request.xhr? ? false : 'application' }
end
You need to set this on your show action.
def show
render :layout => !request.xhr?
end
:)

How can I explicitly declare a view from a Rails controller?

I want to explicitly call a view from my controller.
Right now I have:
def some_action
.. do something ...
respond_to do |format|
format.xml
end
end
... then it calls my some_action.xml.builder view. How can I call some other view? Is there a parameter in respond_to I'm missing?
Thanks,
JP
You could do something like the following using render:
respond_to do |format|
format.html { render :template => "weblog/show" }
end
See the Rendering section of the ActionController::Base documentation for the different ways you can control what to render.
You can tell Rails to render a specific view (template) like this:
# Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
render :template => "weblog/show"
# Renders the template with a local variable
render :template => "weblog/show", :locals => {:customer => Customer.new}
Or even simpler since Rails > 3.0:
render "edit"
You can also pass :action, or :controller if that's more convenient.
respond_to do |format|
format.html { render :action => 'show' }
end
You can modify the internal lookup_context of the controller by doing this in your controller
before_filter do
lookup_context.prefixes << 'view_prefix'
end
and the controller will try to load view/view_prefix/show.html when responding to an show request after looking for all the other view prefixes in the list. The default list is typically application and the name of the current controller.
class MagicController
before_filter do
lookup_context.prefixes << 'secondary'
end
def show
# ...
end
end
app.get '/magic/1`
This GET request will look for a view in the following order:
view/application/show.erb
view/magic/show.erb
view/secondary/show.erb
and use the first found view.
Use render
http://api.rubyonrails.com/classes/ActionController/Base.html#M000474

Resources