Disable some parts of layout in the URL - ruby-on-rails

I'm building my first rails app just right now and now I have a problem.
I have an application layout which includes an header and a footer (and of course the content). Now I have a popup in one view (I use Facebox) which works well, except I don't want to show the footer in the page loaded via ajax.
I don't know how I can do that ... is there a way how I could add something like this option to the URL?
I would already be very happy if you could just give me an idea how to solve this problem.

Do you need to change layout of a page that is shown inside the popup? If so, check please :layout options of the render method. You can use another layout, or no layout at all.
You can do it in your controller. Either create new action for "footerless" page (don't forget to modify routes.rb)
def show_footerless (user/1/show_footerless)
#user = User.find(params[:id])
render "show", :layout => "footerless" # or :layout => nil
end
Or pass extra param to standard action (/user/1?footerless=1)
def show
#user = User.find(params[:id])
if params[:footerless]==1
render "show", :layout => "footerless" # or :layout => nil
else
render "show"
end
end

You can do, when opening the popup
#popup_displayed = true
Then in the layout
<%= render 'layouts/footer' unless #popup_displayed %>

Related

Rails - render :layout => false on new action

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.

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.

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

Ruby on Rails: insert javascript into a page

So.. I have this in the action called when someone clicks the archive button
respond_to do |format|
format.js do
render :update do |page|
page << "alert('You have reached your archive object limit. You have #{remaining} remaining archived objects.');"
end
end
end
But instead of alerting, it just gets rid of the entire page and shows a JavaScript try / catch with that alert message. How do I just do an alert without rendering anything?
Add
:layout => false
in render
Needed to change form_for to form_remote_for to enable ajax
If it's an AJAX call, you may do something like this in your action:
render :text => "<script type='text/javascript'>alert('bla');</script>"

How rails render works in controller? Why something it doesn't use layout?

I tried to write render in an action, but layout behavior is different, why?
def show
# assuming we have a partial in app/views/shared/_panel_show.html.erb
#render "shared/_panel_show" # have layout
#render "/shared/_panel_show" # without layout
#render "shared/panel_show" # Template is missing
#render :partial => "shared/panel_show" # without layout
render :partial => "/shared/_panel_show",:layout => "application" # have layout
end
I want to render a partial and follow controller layout.
The whole point of a partial is that it only renders a part of a view and renders it without any layout.
I would suggest creating a new view (and action in our controller), say shared/full_panel_show, which just renders the partial.
<%= render :partial => 'shared/panel_show' %>
Now in your controller render the new view:
def show
render :action => 'shared/full_panel_show'
end
Depending on what you are doping with the show view, you could just render the partial from it's view instead.
I Have a solution to render a partial with layout in controller
render 'shared/_panel_show', layout: "layouts/application"
just remove the "partial" method then add underscore.

Resources