Show partial from another controller or model - ruby-on-rails

I have a Project that has_many tasks. The Task has a _form.html.erb file that I wanted to show on my show.html.erb file from Project.
But everytime I try this, I am getting the following error:
undefined local variable or method `task'
I am using <%= render 'tasks/form' %> to render the partial.
Also, would
<% #tasks.each do |task| %>
<%= listing.task.name %>
<% end %>
do the trick to show all listings that's associated with the current page?

It looks like you are just missing to pass the task local to the partial.
This should work:
<%= render partial: 'tasks/form', locals: { task: #task } %>

Related

Rendering collections within collection in Rails

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

Cannot render partial of another module and pass variables

I am sorry in advance as I know this should be an easy one but I am stuck. I have a show view for "Category" in which I am trying to display related has_many "Subcategories". I am calling the partial by using the following:
<%= render partial: 'subcategories/subcategory', locals: {category: #category }%>
I have an html file in the Subcategories view folder properly named and the partial view loads. I know this because the partial has the code
<%= #category.name %></p>
which shows the correct Category name within the partial. However, when I try to load any of the subcategory data by calling
<% #subcategories.each do |subcategory| %>
<%= subcategory.name%>
<% end %>
I get the error: NoMethodError in Categories#show, undefined method `each' for nil:NilClass
I'm sorry to ask such a basic question but I will be using partials from related modules extensively in this project.
Replace this:
<%= render partial: 'subcategories/subcategory', locals: {category: #category }%>
with this:
<%= render #category.subcategories %>
Then in the partial app/views/subcategories/_subcategory.html.erb do this:
<%= subcategory.name %>

Rendering Rails partial form in another partial

I have models called project and test. A project has many tests.
In the index.html.erb of tests, I have:
<%= render #tests %>
So I therefore have a file called _test.html.erb, and in it I have:
<%= render "form" %>
I then have a filed called _form.html.erb with:
<%= form_for([#project, #project.tests.build]) do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<% end %>
But I get an exception:
undefined method `tests' for nil:NilClass
So #project is apparently nil. I understand my set up is a bit strange, so I'm not sure how I would refactor this to work?
Instance variables are accessible from any level of rendering.
You can render 100 partials inside each other and you would still have access to those same ivars.
The problem looks like you aren't actually setting #project anywhere.
You probably are looking for "test.project" in your form.
<%= render #tests, project: #project %>
Then
<%= render 'form', project: project %>
The idea is to pass in the #project instance variable as a local variable.
Are both partials in the same view?
You will also have to pass the #project object to the partial and every partial that uses #project.
Example:
<%= render #tests, project: #project %>

Passing local variable to partial inside for each loop rails 3

This is my code for rendering the partial (the #parties collection is being generated correctly, I have tested that):
<% #parties.each do |party| %>
<div class="item">
<%= render 'parties/party', :object => party %>
</div>
<% end %>
And this is the code in the partial:
<%= party.name %>
However, I get the following error:
undefined method `name' for nil:NilClass
I'm at my wits end, someone please help :-|
Also, this is the code for the controller to render the view containing the partial (The controller's called default_controller):
def index
#parties = Party.all
end
Is it of any consequence that this isn't the parties_controller?
I've tried something like below and it worked
<%= render :partial => 'party', :object => party %>
and I can access like party.name. the local variable is named after the partial name which is party here.
Note: Im assuming that your both partials are of parties_controller. So this should work.
Update:
Here is what ive tried with again
class PostsController < ApplicationController
#... ...
def index
#posts = Post.all
#comments = Comment.all #<---- Loading comments from PostsController
#... ...
end
end
#views/posts/index.html.erb
<% #comments.each do |comment| %>
<%= render :partial=>"comments/comment", :object=>comment %>
<% end %>
#views/comments/_comment.html.erb
<%= comment.body %>
And its working :)

ruby on rails render deosn't find the file

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

Resources