0

Rails3.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

「TestAuthor」のような文字列の結果が表示されます

ただし、<%= comment.author_name %>@commentsでレンダリングする部分的な_comment.html.erbを呼び出すと、次のエラーが発生します。

ActionView::Template::Error (undefined method `profile' for nil:NilClass):

次の行を使用して、作成アクション内でauthor_idinを割り当てます。comments_controller

@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(で呼び出され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}" %>&nbsp;is most likely to:</p>&nbsp;<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:&nbsp;&nbsp;</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 %>
4

2 に答える 2

1

私はそれを理解したと思います。パーシャルの1つに、のフォームがありました。@profile.comments.newこれは、nilコメントを。で初期化するものでしたprofile_id。だからそれは物事を台無しにしていた。それを置き換えて、 forをform_for Comment.new追加すると役に立ちました。hidden_fieldprofile_id

于 2012-12-21T04:17:56.843 に答える
0

コントローラとビューにもあるコードを書いていただけませんか?また、パーシャルを呼び出す前に、@ commentにあるものをデバッグできますか?

あなたの見解でこのようなことをする前に:

<%= render :partial => "comment", :collection => @products %>

コードにコメントして、次のようなことを行います

<%= debug @products %>
<%# render :partial => "comment", :collection => @products %>

また、やってみたくなるかもしれません

<%# render :partial => "comment", :collection => @products.all %>

製品のコレクションではなくActiveRecordリレーションシップを渡している可能性があり、それがエラーである可能性があるためです。

于 2012-12-10T08:57:19.587 に答える