2

誰かが私のためにこれに光を当てることができますか?

#の未定義のメソッド `first_name'

これがshow.htmlです

<p id="notice"><%= notice %></p>
<div id="container">
<p>
  <b>First name:</b>
  <%= @artist.firstname %>
</p>

<p>
  <b>Second name:</b>
  <%= @artist.surname %>
</p>

<p>
  <b>About:</b>
  <%= @artist.about %>
</p>
<div id="comments">

  <h2>Comments</h2>

<%= render :partial => "shared/comment", :collection => @artist.comments%>

</div
</div>

<%= render :partial => "image", :collection => @artist.images %>
<%= link_to 'Edit', edit_artist_path(@artist) %> |
<%= link_to 'Back', artists_path %>
<%= link_to 'show', images_path %>

これが部分的です

 <div class="comment">



     <p>


      <span class="commentator"><%= comment.commentator.display_name %> 

    say's</span>


      <%= comment.comment %>


      </p>



    </div

これが友達ビューです

class Friends < ActiveRecord::Base
    attr_accessible :firstname, :surname
    has_many :comments, :as => :commentator, :class_name =>"Commentable"
    def display_name
    "#{self.firstname} #{self.surname}"
    end


end

これは友達のコントローラーです

class FriendsController < ApplicationController
  # GET /friends
  # GET /friends.xml
  def index
    @friends = Friend.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @friends }
    end
  end

  # GET /friends/1
  # GET /friends/1.xml
  def show
    @friend = Friend.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @friend }
    end
  end

  # GET /friends/new
  # GET /friends/new.xml
  def new
    @friend = Friend.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @friend }
    end
  end

  # GET /friends/1/edit
  def edit
    @friend = Friend.find(params[:id])
  end

  # POST /friends
  # POST /friends.xml
  def create
    @friend = Friend.new(params[:friend])

    respond_to do |format|
      if @friend.save
        format.html { redirect_to(@friend, :notice => 'Friend was successfully created.') }
        format.xml  { render :xml => @friend, :status => :created, :location => @friend }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @friend.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /friends/1
  # PUT /friends/1.xml
  def update
    @friend = Friend.find(params[:id])

    respond_to do |format|
      if @friend.update_attributes(params[:friend])
        format.html { redirect_to(@friend, :notice => 'Friend was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @friend.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /friends/1
  # DELETE /friends/1.xml
  def destroy
    @friend = Friend.find(params[:id])
    @friend.destroy

    respond_to do |format|
      format.html { redirect_to(friends_url) }
      format.xml  { head :ok }
    end
  end
end

友達がアーティストのページにコメントを残せるようにしようとしていますが、上記のエラーが発生し続けます。

私はRubyを初めて使用するので、何かを省略してしまったことをお詫びします。

4

3 に答える 3

4

基本的に、railsはデータベースを調べて、モデルのどのフィールドにあるかを把握します。したがって、移行が実行され、first_nameがdbテーブルに存在することを確認してください。

また、友達は複数形です。Railsでは、テーブルは複数形(friends)、モデルは単数形(Friend)、コントローラーは複数形(FriendsController)です。この慣習に反しないことが最善です。モデルの名前を変更して、何が起こるかを確認してください

于 2012-05-04T16:55:04.100 に答える
0

ビューがこれらの変数にアクセスFriendできるように、クラスの2行目が必要です。attr_accessible :firstname, :surname

于 2012-05-04T16:51:06.483 に答える
0

このエラーは、first_nameがデータベースに存在しないデータベースに関連しています。移行を慎重に実行してください。

于 2012-05-04T17:01:02.077 に答える