Right now I have a rails partial that looks like this:
<%= render :partial => "/talk/partials/comment", :collection => #comments, :locals => {:votes => #votes} %>
I am passing in a collection of comments and another local variable.
That comment partial then goes right into using the comment variable and works fine.
I have since made another partial called '/talk/partials/comment_2014'. When I try this, I am getting the error undefined local variable or method 'comment'. From what I can gather, when I have a different partial name, something with the variable also changes. I would like to keep the same comment variable for the new partial ''/talk/partials/comment_2014'. How would I go about doing this?
Something I tried which did not work was the following:
<% #comments.each do |comment| %>
<%= render :partial => "/talk/partials/comment_2014", comment: comment, :locals => {:votes => #votes} %>
<% end %>
which did not work either.
You can do it this way
<% #comments.each do |comment| %>
<%= render "/talk/partials/comment_2014", comment: comment, votes: #votes %>
<% end %>
Or
<% #comments.each do |comment| %>
<%= render partial: "/talk/partials/comment_2014", locals: { comment: comment, votes: #votes } %>
<% end %>
Notice in the second way the comment is inside the locals.
Related
This is driving me crazy, I thought it should be something very simple but I've spent all day trying to figure it out and getting nowhere. I've looked all over, there's something I must be missing.
I want to render a partial for a collection, within a partial from a collection.
Eg. I have a collection of entries which I want to render in my feed partial, I want feed to come from a collection of feeds, such that the page displays all entries in all feeds.
How can I do this?
Something like:
controller:
#feeds = Feeds.all
allmyfeeds.html.erb
#feeds.each do |feed|
<%= render 'feed' %>
<% end %>
or
<%= render 'feeds', collection: #feeds %>
_feed.html.erb
<%= render 'items', collection: #items %>
_item.html.erb
<%= item.summary.html_safe %>
But I really don't know.
Partial naming conventions will really help you out here...
In allmyfeeds.html.erb (which i'm guessing is actually feeds/index.html.erb), instead of this...
#feeds.each do |feed|
<%= render 'feed' %>
<% end %>
You can simply call this...
<%= render #feeds %> # this will invoke _feed.html.erb for every feed in the collection, automatically
Inside your _feed.html.erb partial...
# Note: You may need to change `items` to something like `items/items` if it is not located in the same directory
<%= render partial: 'items', locals: { items: feed.items } %>
Then in your items partial, you will have access to items, which is a collection of items for that particular feed
Somehting like:
<% #feeds.each do |feed| %>
<%= render partial: 'feed', locals: { feed: feed } %>
<% end %>
In your _feed.html.erb:
<h1><%= feed.id %> </h1>
<% feed.entries.each do |entry| %>
<&= render partial: 'entry', locals: { entry: entry } %>
<% end %>
In your _entry.html.erb:
<h2><%= entry.id %></h2>
I'm assuming your Feed model has a has_many :entries association, if it's not the case (and what you want is to render the same collection of #entries in each feed), then you just need to pass the #entries collection to the _feed.html.slim partial, like:
render partial: 'feed`, locals: { feed: feed, entries: #entries }
and update your _feed.html.erb
I'm new to ruby on rails.
In views/events I have "_form.html.erb" which is rendered in "new.html.erb" by this code:
<%= render "form" %>
Now I want to render "_form.html.erb" in "index.html.erb" which is in the same folder(views/events).
But I get the error "missing template".
I guess I have to add some thing to controller, please help me to render form in other pages of views...
You "usually" don't render a form in an index action. Most form partials are setup semantically to expect a #my_resource, but if you're doing everything the rails way you're not going to have a instance variable during your index action. There's a number of ways you can do this but this is probably the quickest.
You probably have some collection (let's pretend you're using books) in your index action:
#views/books/index.html.erb
<% #books.each do |book| %>
...
<%= render "form" %>
...
<% end %>
You can just set an instance variable somewhere prior to rendering the form:
#views/books/index.html.erb
<% #books.each do |book| %>
<% #book = book %>
...
<%= render "form" %>
...
<% end %>
Another way to do it would be through passing in some locals to a partial. You'd have to change all of your references in _form to use a local variable instead. Then you can call render like this:
<%= render :partial => "form", :locals => {:book => book } %>
You can try
<%= render "events/form" %>
I had this problem before and this solved
I render an instance of a model into a partial with the following code <%= render #places %>.
I created _places.html.erb. I thought I could use the following code since the render method is supposed to iterates trhough the places collection :
<li>
<%= link_to place.name, place %>
</li>
I get this error : undefined local variable or method 'place' for #<#<Class:0x007fc1c0a4e6b8>:0x007fc1c05050a8>
I have to use to make it work :
<% #places.each do |place| %>
<li>
<%= link_to place.name, place %>
</li>
<% end%>
try this:
<%= render :partial => 'places.html', :collection => #places, :as => :place %>
Or, if you have the partial file as singular name, you can do it like this(without the variable :as)
<%= render :partial => 'place.html', :collection => #places %>
I'm creating an application with ruby on rails where I have an items/_item.html.erb. Inside the partial is a yield statement so i can add extra content as needed. In this case, I want to add a specific button to item depending on what view calls partial.
This is what I've tried and it renders the partial, but it does not render the block:
_item.html.erb
<%= yield if block_given? %>
<div>
<%= item.name %>
</div>
someview.html.erb
...
<% render(:partial => 'items/item', :collection => current_user.items do %>
<%= "HELLO" %>
<% end %>
...
I have also tried using content_for and some other stuff with no success. Is there a way to be able to render specific content inside a partial via yield? I'm currently using Rails3
EDIT:
I've found out that it's the :collection hash that makes it impossible insert the block.
Both of this pieces of code work:
<%= render :layout => 'items/item' do %>
Hello world
<% end %>
<%= render :layout => 'items/item', :locals => {:item => current_user.items.first} do %>
Hello world
<% end %>
This means that if i do a .each i could accomplish what I want but it would be ugly code. Anyone know a way around this?
content_for should work fine in this case. Here is the code I just double checked locally.
somewhere.html.erb
<% content_for :foobar do %>
fubar
<% end %>
_item.html.erb
<% if content_for? :foobar %>
<%= yield :foobar %>
<% end %>
I have the following in a view (.html.erb) :
<% #posts = GetAllPostsFunctions %> (removed for berivity)
<% #posts.each do |post| %>
<%= post.title %>
<%= render :partial => "posts/post_show" %>
<% end %>
the posts_show partial has the following:
....
<td><%=h #post.title %> </td>
But I get the following error
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.title
Any ideas?
You can also simply things by using the :collection for render :partial. Which pass each item in the value for :collection to a local variable sharing the name of your partial.
<% #posts = GetAllPostsFunctions %> (removed for berivity)
<%= render :partial => "posts/post_show", :collection => #posts %>
In this case, Rails will render post_show for each item in #posts with the local variable post_show set to the current item. It also provides handy counter methods.
Successfully using this approach would require renaming the app/views/posts/_post_show.html.erb partial to app/views/posts/_post.html.erb to or changing every occurance of post in your partial to post_show. If you renamed the partial to the conventional _post.html.erb which will then allow you to simply do:
<%= render :partial => #posts %>
Which will render the partial for every single post in the #posts variable.
Since the post variable in the each loop is a locale variable you have to make it available to the partial:
<%= render :partial => "posts/post_show", :locals => {:post => post} %>
You can then access the title through the local variable post:
<td><%=h post.title %> </td>
You could also simplify the whole thing by rendering the posts as collection. Take a look at the Rails documentation for more information:
http://api.rubyonrails.org/classes/ActionController/Base.html#M000658
It doesn't look like you're setting the #post of the partial, so when it goes to evaluate the partial it gets a null reference.
Alternately, ensure that your post fetching functions are actually returning something
I'm not positive, but I think in the partial you have to do post.title not #post.title
Sorry if I misunderstood you, I'm new to rails.