Rails rendering layout only? - ruby-on-rails

Just trying out a simple rails app, mostly going for an API backend with JSON, heavy client side app. So what i want to do is only render the layout, and have javascript code handle the url, and make the ajax request to get the json data. The following seems to work:
respond_to do |format|
format.html { render :nothing => true, :layout => true }
end
However, since nothing is meant to render nothing, it feels kinda wrong. Is there a more proper way to just render the layout? Note that my layout does not have a yield.

Well, this worked for me in Rails 4.0:
render :text => "", :layout => true

(Copying Vasile's comment for better visibility.)
For rails 5.1, in order to render the layout without requiring a view template, you need to use
def index
render html: '', layout: true
end
or with a custom layout
def index
render html: '', layout: 'mylayout'
end
Per tfwright, this will also work:
def index
render html: nil, layout: true
end
However, the following will not work:
render text: '', layout: 'mylayout' will give you an error because the view template index.html.haml does not exist.
render nothing: true, layout: 'mylayout' will give you an error because nothing:true is deprecated, and the index template does not exist (however, this works in rails 4.2)
render body: '', layout: 'mylayout' will render '' (no layout)
render plain: '', layout: 'mylayout' will render '' (no layout)

render :file => "layout_file", :layout => false

Try this, this will render the response to a file called sample.html which could be a static html file.
and also you could have this file in a common location, so that you could loaded it to all the actions
have your static content in this page, and if you need a dynamic page you could have a .erb page too
in your method
def index
#posts = Post.all
respond_to do |format|
format.html {render :file => "posts/sample"}
format.json { render json: #posts }
end
end
/post/sample.html
HTH

I needed something similar-- to display a layout template with no partial (where partials are handled clientside).
rails 4.2.0
respond_to do |format|
format.html { render text: nil, layout: true }
end

I believe if your application is just fetching JSON from the server, the format should be json and not html.
respond_to do |format|
format.json #your_collection
end
Or put the format for all actions in your controller and just respond with the objects.

Related

How to share a view between formats with Rails

I have a Rails controller which provides both HTML and PDF responses, and thus I have view.pdf.haml and view.html.haml files. These are either identical or extremely close to identical.
How can I have Rails use a single view for multiple formats?
You can specify what format to render with :formats option:
# Both will render view.html.haml
respond_to do |format|
format.html { render :view }
format.pdf { render :view, formats: :html }
end
https://guides.rubyonrails.org/layouts_and_rendering.html#the-formats-option
Do you mean have the same action serve up different resource types - i.e. both a web page and a PDF? If I understand the problem, you can use the respond_to method.
def show
#object = Object.find params[:id]
respond_to do |format|
format.html
format.pdf { ..code to render the pdf.. }
end
end
That ..code to render the pdf.. is the tricky part. There are PDF gems that support inline rendering, but you may end up using send_data or similar to set a file name, disposition, etc and deliver the PDF document to the end user.

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.

Why is application.html.erb being called on a js response?

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

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!

Is there an AJAX function that can just GET a rails partial?

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") %>');

Resources