Reputation: 2061
I'm not sure why i can't get my index action to pass to my view. Basically i have a controller called "jobs" with a partial called "recent jobs" and i want to display that "recent jobs" partial in another contollers view called "static_pages".
I thought that all i would have to do is call the below in my static_pages/home.html.erb:
<%= render partial: "jobs/recent_jobs" %>
but here's the error i get. apparently @jobs is nil.
ActionView::Template::Error (undefined method `each' for nil:NilClass):
2: <div class="homepage_recent_jobs">
3: <div class="container">
4:
5: <% @jobs.each do |job| %>
6: <% job.title %>
7: <% end %>
8:
here's my partial in the jobs controller
<div class="homepage_recent_jobs">
<div class="container">
<% @jobs.each do |job| %>
<% job.title %>
<% end %>
</div>
</div>
jobs index action
def index
set_link_order_and_counters
@jobs = Job.all
# calls scopes
if params[:experience].present?
@jobs = @jobs.by_experience(params[:experience])
end
if params[:num_days_past].present?
@jobs = @jobs.by_num_days_past(params[:num_days_past].to_i)
end
@jobs = @jobs.paginate(page: params[:page], per_page: 5)
end
Upvotes: 1
Views: 43
Reputation: 33471
As you're working with @jobs
, and rendering static_pages#home
you need to define it first, you've defined it only in jobs#index
, and that's why when the view is rendered the value is nil
.
Adding @jobs
in the proper controller should work:
class StaticPagesController < ApplicationController
def home
@jobs = Job.all
end
end
And note with <%= render 'jobs/recent_jobs' %>
Rails infers is a partial file, no need to use the partial
option. Also to print the job's title you would need to use <%= %>
.
Upvotes: 2