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
Related
I am using render :layout => false for my new action.
All works fine, until I submit the form with validation errors. The create action calls render :new and I see the form, with the bullets outlining the errors. The issue is that the form now renders with application layout and seems to be ignoring the render :layout => false.
From what I can tell I believe this is because I am not actually redirecting to :new, just rendering the :new view whilst still in the create action. I cant add render :layout => false to the create action, because rails only allows one render per action.
Is there a way to get the form with validation errors to not use the application layout? I still want the bullets with the validation errors.
Apologies, looks like I worked this out. Thought I had tried this but must have been a syntax error before.
The solution is to add render :new, :layout => false to your create action.
Iam not sure, but you can use a
layout :false, only: [:create]
hope this may help.
I am trying click on a link to call this action.
<% link_to "popup", :action => 'user_logs_out', :controller => 'application'%>
In my application controller I have this action:
def user_logs_out
gon.display_sign_out_popup = true
end
How can I call this action? It always shows template error. Since the action simply needs to set a boolean value, I don't feel a need to have view file for it.
If you really want an action to render nothing, you can use render :nothing => true.
def user_logs_out
display_sign_out_popup = true
render :nothing => true
end
However, there is almost certainly no reason why you would want to do this.
You have to put
def user_logs_out
display_sign_out_popup = true
respond_to do |format|
format.js
end
end
You also must put :remote => true in your link_to
...and if you are trying to learn authentication from scratch I would recommend Hartl's free tutorial: http://ruby.railstutorial.org/book/ruby-on-rails-tutorial?version=3.2#cha-modeling_users
My application in many action use render :nothing => true. I want be DRY. How can I say to rails by default render nothing for a particular controller?
You could do something like:
# put this in a module, or your ApplicationController
def self.nothing_for *actions
before_filter :only => actions do
render :nothing => true
end
end
# This would go in your specific controllers
nothing_for :show, :new, :create
This might do what you need:
before_filter :render_nothing
private
def render_nothing
render :nothing => true
end
before_filter and not after_filter because the latter is executed after the action finished, which means, after the content was rendered.
Source: http://ap.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html#M000128
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
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