I have a collection of partials being rendered using a layout for each element (wrapping in a container). However, when rendering the collection, an outer 'container' is also added (it appears to be adde to each render, despite no layout being specified.
Example:
# index.html.erb
<%= render :partial => 'sprockets' %>
# _sprockets.html.erb
<%= render :partial => 'sprocket', :layout => 'container' %>
<%= render :partial => 'sprocket', :layout => 'container' %>
<%= render :partial => 'sprocket', :layout => 'container' %>
# _sprocket.html.erb
...
# _container.html.erb
<div class="container"><%= yield %></div>
Gives:
<div class="sprocket">
<div class="sprocket">
...
</div>
<div class="sprocket">
...
</div>
<div class="sprocket">
...
</div>
</div>
I seem to remember being able to do this in Rails 2.3.8. Note the above is a simplification of my code (I'd like to keep the layouts and multi-partial format). Any ideas what I'm doing wrong? Thank!
You are probably exploiting a quirk in ActionView. To the best of my knowledge, layouts are not meant to be used with partials like that. My guess is, every time you do :layout => 'container', it sets the same instance variable. Since ActionView renders partials inside-out (innermost partial gets rendered first), the last used value for :layout also gets used higher up the chain.
Maybe doing something like
# index.html.erb
<%= render :partial => 'sprockets', :layout => false %>
will help.
Still I'd say this is not a proper way to get the results you want.
Related
I'm trying to build out an app and I started getting confused when to use a partial as opposed to simply refactoring code. In other words, when should I use 'render' and 'render partial: ........"
And if I put something in the "shared" folder under "views" does that make it a partial? Not sure when to use these different folders. Thanks a whole bunch!
Normally you use 'render' for just move some html codes, like "footer".
If you want the partial with its own layout or pass variables in it. We will use 'render :partial'
<%= render "footer" %> # Basic usage
<%= render "shared/footer" %> # _footer.html will be placed in "shared/_footer.html"
<%= render :partial => "sidebar", :layout => "sidebar_layout" %>
# It will using "_sidebar_layout" as a layout template for "_sidebar.html"
<%= render :partial => "form", :locals => { :post => #post } %>
# Passing #post variable as post in form partial
Reference: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
here is what I'm trying to do:
page.replace_html('manage_categories_list', :partial => "/categories/categories", :layout => :modal)
But, I get this error on the above code:
NoMethodError (undefined method `include?' for :modal:Symbol):
What I want to be able to do, is have template HTML for a modal dialog window. And set the header (h2) and the body (div) of that modal with any partial. =\
modal.html.erb:
<div class="fixed_modal">
<div class="modal_header">
<%= yield :header %>
</div>
<div class="modal_body">
<%= yield %>
</div>
</div>
The partial I'm trying to render:
<% content_for :header do %>
Manage Categories
<% end %>
.... rest doesn't matter as it just goes into the yield
I'm using rail 2.3.14
Most likely, you need to change it to ..., :layout => 'modal':
page.replace_html('manage_categories_list', :partial => "/categories/categories", :layout => :modal)
The #replace_html method accepts options like #render. For the :layout option, you either need to pass true, or a string layout file name (See the docs for #render).
The layout argument will be the 'xyz' part of the filename in app/views/layouts/xyz.html.erb.
The argument to :layout can't be a symbol try
:layout=>'modal'
This assumes you have a layout in app/views/layouts/modal.html.erb
I am new to Ruby on Rails and i am working through a few example applications in the O'Reilly Head First Rails book. In one of the examples there is a page made up of three partials. The middle partial is a list of items. There is a link right below this section that, when clicked, should refresh the div containing that partial. The book is running examples based off of Rails 2.3 i believe and i am using Rails 3.1. This is the example that the book is giving me:
routes.rb:
map.connect '/flights/:flight_id/seats', :action=>'flight_seats', :controller=>'seats'
seats_controller.rb:
def flight_seats
#flight = Flight.find(params[:flight_id])
render :partial => "flights/seat_list", :locals => {:seats => #flight.seats}
end
show.html.erb:
<div id="seats">
<%= render :partial=>"seat_list". :locals=>{:seats=>#flight.seats} %>
</div>
<$= link_to_remote("Refresh Seats", :url=>"/flights/#{#flight.id}/seats", method=>"get", :update=>"seats") %>
This example is also using prototype.js since that's what Rails 2.3 came with built in. Rails 3 has jQuery as the default JavaScript library. (not sure if that makes a big difference)
Here is what i have so far. This is getting the contents of the partial correctly, it's just not updating the "seats" div after the AJAX call gets the partial. My code:
routes.rb:
match 'flights/:flight_id/seats' => 'seats#flights_seats'
seats_controller.rb:
def flights_seats
#flight = Flight.find(params[:flight_id])
render :partial => "flights/seat_list", :locals => { :seats => #flight.seats }
end
show.html.erb:
<div id="seats">
<%= render :partial => 'seat_list', :locals => { :seats => #flight.seats } %>
</div>
<%= link_to "Refresh Seats", "/flights/#{#flight.id}/seats", :remote => true %>
Any idea why my <div id="seats"> won't refresh with the updated partial? I'm betting there is but i'll ask anyway, is something wrong with my code?
The :remote => true option is a bit weird if you aren't returning JSON data. You can wrap your HTML in a JSON object, though, which is what I typically do. Or if you want something closer to your existing code something like this should work for you:
<%= link_to "Refresh Seats", "/flights/#{#flight.id}/seats", :class => "refresh-seats" %>
In your javascript somewhere:
$(document).delegate(".refresh-seats", "click", function(e){
e.preventDefault();
$("#seats").load(this.href);
});
I would like to render the contents of an action (e.g new_sub_batch) inside a div.
I tried
<div id="newBatch">
<%= render :template => 'new_sub_batch.html.erb' %>
</div>
but nothing is displayed.
I even tried <%= render :action => 'new_sub_batch' %>..still nothing.
Any suggestion??
Thanks a lot
What you want are partials. Distil the common markup that both views will use into a single file, and prefix its name with an underscore. Then call render :partial => 'filename', where filename is the name of the partial without the underscore.
In your case, the code you pull out of new_sub_batch.html.erb might go in a _batch.html.erb partial, in the same directory as your other sub_batch views. You would render this partial with:
render :partial => 'batch'
In Rails3, you can simply use render 'batch'.
If you want to pass a variable to the partial, you can do so via :locals. Assuming you have a #sub_batch variable you want to pass, your call would look something like this:
render :partial => 'batch', :locals => { :sub_batch => #sub_batch }
While this doesn't strictly answer your question, I believe within the ruby-on-rails tag it's more important to explain the Rails Way, rather than help you do it the wrong way.
Try file render:
<div id="newBatch">
<%= render :file => 'directory/new_sub_batch.html.erb' %>
</div>
I have a loop in my view that renders many partials: each partial is a simple toggle to perform a save/unsave operation on a record. Each partial toggles the boolean field using javascript then updates itself, and the controller finishes by saying:
$ controller
render :partial => "save_unsave_buttons", :locals => {:matching => #matching}, :layout => false
# view
<div id=<%= "save#{match.id}" -%>>
<%= render :partial => "save_unsave_buttons", :locals => {:matching => match} %>
</div>
When the controller renders the save_unsave_buttons partial, it isn't rendering to the right div; it just updates the partial for the first record it finds. How can I ask the controller to render the save_unsave_buttons partial to the div with the right id?
Thanks!
Looking at only the little info that you provided for your problem i guess the culprit in the code is the div tag.
The div tag should be within the partial.
EDIT: What your code does is it creates a single div with id save(the first match.id) and renders the partial within it. If I understood you correctly you need a div for each match. For that the div itself should be within the partial.
So pass the match.id to the partial in a collection. Your view should be:
# view
<%= render :partial => "save_unsave_buttons", :locals => {:matching => match, :collection => {#match_id => match.id} } %>
and in your partial _sav_unsave_buttons.html.erb:
<% content_tag :div, id => "save#{#match_id}" do %>
#YOUR CODE GOES HERE!
<% end -%>