我的 Rails 3.2 应用程序中有一个自我连接。它似乎在控制台中工作,但在视图中似乎我无权访问 class_name 的任何关联。这是我的代码:
class Comment < ActiveRecord::Base
belongs_to :profile
belongs_to :author, :class_name =>"User", :foreign_key => "author_id"
def author_name
author.profile.name
end
end
class Profile < ActiveRecord::Base
belongs_to :user
has_many :comments
end
上面的区别是个人资料有很多关于它的评论。但是用户可以作为作者对另一个配置文件发表评论。
因此,如果我在控制台中并运行以下内容:
Comment.first.author_name
我会得到一个字符串结果,如“测试作者”
但是,如果我调用<%= comment.author_name %>
使用@comments 呈现的部分 _comment.html.erb,则会收到以下错误:
ActionView::Template::Error (undefined method `profile' for nil:NilClass):
author_id
我在comments_controller
create 操作中分配了这一行:
@comment = Comment.new(params[:comment].merge(:author_id => current_user.id))
配置文件和评论控制器:
class CommentsController < ApplicationController
def create
@comment = Comment.new(params[:comment])
if @comment.save!
@profile = @comment.profile
Resque.enqueue(CommentWorker, @comment.id)
respond_to do |format|
format.js { }
end
else
respond_to do |format|
format.js { render 'fail.js.erb' }
end
end
end
end
class ProfilesController < ApplicationController
def show
@profile = Profile.find(params[:id])
@user = User.find(@profile.user_id)
@comments = @profile.comments
end
end
视图(按部分加载的顺序:
内profiles/_logged_in.html.erb
(称为 in profiles/show.html.erb
)
<%= render :partial => 'profile_cred' %>
内profiles/_profile_cred_in.html.erb
<% if current_user_profile?(@profile) %>
<%= render :partial => "comments/auth_user_comments" %>
<% else %>
<%= render :partial => "comments/user_comments" %>
<% end %>
内profiles/_auth_user_comments.html.erb
<% if @comments.count == 0 %>
<p id="commentIntro">You have no comments yet, <%= "#{@profile.first_name}" %>.</p>
<% elsif @comments.count > 0 %>
<p id="commentIntro"><%= "#{@profile.first_name}" %> is most likely to:</p> <ul id="commentInfo"><%= render @comments, :locals => {:comment_count => @comments.length} %>
<% end %></ul>
内profiles/_user_comments.html.erb
<div id="newComment">
<p id="commentIntro"><%= @profile.first_name %> is: </p><%= render :partial => 'comments/form', :locals => {profile: @profile} %>
</div>
<div id="list">
<ul id="commentInfo">
<% if @comments.count > 0 %>
<%= render @comments %>
<% end %>
</ul>
</div>
内comments/_comment.html.erb
<li id="<%= comment.id %>" class="comment">
<span title="<%= comment.author_name %>"><%= comment.body %><% if current_user_profile?(@profile) %><p class="deleteSup"><%= link_to 'x', comment_path(comment.id), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this comment?" %></p><% end %></span>
我不知道为什么这在控制台中有效,但在视图中无效。有任何想法吗?
编辑:
comments/user_comments
例如,如果我正在查看我在另一个人的个人资料上给出的评论,则会在渲染代码时引发错误。当我调试这里是我的输出:
#<Comment id: 32, profile_id: 1, author_id: 45, body: "test", created_at: "2012-12-15 05:12:00", updated_at: "2012-12-15 05:12:00">,
#<Comment id: nil, profile_id: 1, author_id: nil, body: nil, created_at: nil, updated_at: nil>]
我认为这是由于以comments/user_comments
部分形式实例化一个新的评论引起的,但我不知道。这是表单代码:
<% if logged_in? and not current_user_profile?(profile) %>
<%= form_for(profile.comments.new, :remote => true, :html => {:id =>"new_comment"}) do |f| %>
<%= f.hidden_field :profile_id, :value => profile.id %>
<%= f.hidden_field :author_id, :value => current_user.id %>
<div class="holds">
<%= content_tag(:span, "ex: change the world", :class =>"holder_comment") %>
<%= f.text_field :body, :autofocus => true %>
</div>
<%= f.submit 'Add' %>
<% end %>
<% end %>