Render :partial with :layout don't work in view
Instead of using
<%= render :partial => 'partial_test', :layout => 'partial_test' %>
what i needed to use was
<%= render :partial => 'partial_test', :layout => 'layouts/partial_test' %>
Rails render of partial and layout in controller
Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.
This may be the reason your code is not working you can use rendering template.
Template rendering works just like action rendering except that it takes a path relative to the template root. The current layout is automatically applied.
if resource.company_form
render :template => "shared/company_signup_form"
else
render :template => "shared/individual_signup_form"
end
** REMOVE UNDERSCORE from your partail name because you are using this as a template.
Hope this works !
Render a partial layout in rails
First of all - always downcase on file names! Never use Design or MyCoolStory, in rails we use convention over configuration and that means snake_case which is everything downcased and separated with _under_scores
To your render issues:
There is a great documentation where you'll find anything you need: http://guides.rubyonrails.org/layouts_and_rendering.html
Let me help you a little.
Rails is looking for a Layout. By default it will be expected in /app/views/layouts/application.html.erb (there is nothing wrong with keeping it named as application). A Layout is the whole HTML Frame you will need. Within the Layout there (should) always be a yield
block.
A YieldBlock in rails is where your templates are rendered into.
so basically a layout-file can look like this (i use haml for easier reading)
%html
%head
=render "shared/head"
%body
.wrapper
%nav.navigation=render "shared/navigation"
.main_content
=yield
%footer.foot=render "shared/footer"
This means you are having 3 partial-templates in /app/views/shared named _head.html.erb, _navigation.html.erb and _footer.html.erb
That's the rails way.
Further informations
If you are planning to have a multi-design app, you should structure your views in total like
- /app/views/design1
- /app/views/design2
- /app/views/design3
- /app/views/shared
and set the lookup path in your controller like this
prepend_view_path "#{Rails.root}/app/views/#{design_path}"
def design_path
current_page.design_name
end
By then, all views will be looked up into their specified folder (Spree multi_store engine, is doing like this, for an example)
Rails render partial from controller
The better way would be to call the partials from index.html.erb
<% unless params[:check].nil? %>
<%= render :partial => '/layout/check' %>
<% else %>
<%= render :partial => '/layout/not_check' %>
<% end %>
so your def index would look like this
def index
respond_to do |format|
format.html
end
end
I did not understand what you are trying to do but partial which are related to controller/actions should not be in layout unless they are serving some layout.
Render :layout is searching for partial instead of layout
render
works differently in controllers than it does in views. In controllers, it's primarily for rendering action templates, while in views, it's primarily for rendering partial templates. When you want to render a specific layout for an action, you have a few options, but all of them are in the controller.
If you want every action in a particular controller to use that layout, you can either specify layout 'something'
in that controller (usually near the top) or for a ApplesController
, you can create a new layout in app/views/layouts/apples.html.erb
and this will automatically be used as the default layout for the ApplesController
.
If you want just a single action in a controller to use that layout, you can use your render layout: 'something'
inside of a controller action, where the action to render is implied to be the current action.
Links from the Rails docs:
- Action Rendering
- Partial Rendering
- Nested Layouts
how to render partial on everything except a certain action
Replace your render with this:
<%= render 'layouts/header' unless @disable_nav %>
Then you can simply set disable_nav
to true in any controller action you like:
def landing_page
@disable_nav = true
end
As a before_filter
, which I'd encourage over the above:
application_controller.rb
def disable_nav
@disable_nav = true
end
my_controller
before_filter :disable_nav, only: [:landing_page]
Rendering a partial in rails, in two separate places, from two different controllers
Should be with an equal sign: <%= render ... %>
.
<% %>
executes Ruby code, but does not render a result. Whereas <%= %>
executes and renders.
Rails 5: Rendering a partial to the application layout
(migrating from comments)
What if you created a show.html.erb
for that controller and placed render :partial
there? It should work.
Explanation: Partials are to be used from "big" views. So, they are not wrapped in the layout on purpose!
Related Topics
Rails 3.2.8 - How to Get the Week Number from Rails
Recommended Two-Way Encryption Gems for Ruby
How to Disable All Form_For Input Fields in Ruby on Rails App
Save Image with Mechanize and Nokogiri
How to Split a String into Consecutive Substrings of Length at Most 3 in All Possible Ways
Parsing Date from Text Using Ruby
In Ruby How to Automatically Populate Instance Variables Somehow in the Initialize Method
How to Use Us-Style Dates in Rails Using Ruby 1.9
How to Increment/Decrement a Character in Ruby for All Possible Values
Ransack, Postgres - Sort on Column from Associated Table with Distinct: True
Array Typeerror: Can't Convert Fixnum into String
Clicking a Button with Ruby Mechanize
Ruby: Yield Block from a Block
Find Number of Bytes a Particular Hash Is Using in Ruby
Trying to Compare Two Text Files, and Create a Third Based on Information
Is There an Equivalent of Array#Find_Index for the Last Index in Ruby