我實現了一個保存按鈕的工作,它做工精細,現在我想列出一些在工作索引頁保存工作爲了這個,我有這樣的代碼Rails的多對多關聯
<h3>Saved Jobs</h3>
<ul>
<% @user.saved_jobs.limit(5).order(:created_at).reverse_order.each do |saved_job| %>
<li><%= link_to saved_job.job.title, saved_job.job.url %>
<span class="delete_button">
<%= link_to "X", saved_job, :method => :delete, :remote => true %></span></li>
<% end %>
</ul>
<%= link_to "see all", saved_jobs_path %>
但是當我希望獲得的職位索引頁我得到這個錯誤undefined method
saved_jobs' for nil:NilClass
這個我saved_jobs控制器
class SavedJobsController < ApplicationController
before_filter :authenticate_user!
def index
@saved_jobs = SavedJob.find_all_by_user_id(current_user.id)
end
def create
@job = Job.find(params[:saved_job][:job_id])
current_user.save_job!(@job)
respond_to do |format|
format.html { redirect_to @job }
format.js
end
end
def destroy
@job = SavedJob.find(params[:id]).job
current_user.saved_jobs.find(params[:id]).destroy
respond_to do |format|
format.html { redirect_to @job }
format.js
end
end
end
,這是我的用戶控制器
class ProfilesController < ApplicationController
before_filter :authenticate_user!
def show
@user = User.find_by_slug(params[:id])
if @user
@posts = Post.all
render action: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
def index
@users = user_scope.paginate(page: params[:page], per_page: 2)
end
private
def user_scope
current_user ? User.where.not(id: current_user.id) : User.all
end
end
你'@ user'變量似乎同等零,還你可以/應該將'.saved_jobs.limit(5 ).order(:created_at).reverse_order.each do | saved_job |'到您的控制器,而不是通過created_at排序和反轉順序,您可以使用降序。你應該創建一個可以重用的範圍。在你看來,你應該只看到'@ user_jobs.each' – mymlyn 2014-10-04 23:09:50
哪個控制器和動作渲染你發佈的視圖? – John 2014-10-04 23:15:14
saved_jobs控制器 – userails 2014-10-04 23:16:19