0

私は RoR 開発の比較的素人 (約 1 か月の経験) であり、この時点まで問題なく別のコントローラーから変数を参照してきました。ブログ用の CMS を開発していますが、ブログの作成者のユーザー名をインデックス ページまたはショー ページに表示できません。「nil:NilClass に対して未定義のメソッド `username'」というエラーが表示されます。ただし、IDは取得できます。私のコードのエラーを指摘してくれる人はいますか? 大変ありがたく存じます。

これはブログのモデルです:

    class Blog < ActiveRecord::Base
    attr_accessible :post, :title

    has_one :users
    belongs_to :user

        end

これは、ブログ コントローラの show および index セクションです。

    def index
      @users = User.all
      @blogs = Blog.all


     respond_to do |format|
       format.html # index.html.erb
       format.json { render json: @blogs }
       end
         end

       # GET /blogs/1
       # GET /blogs/1.json
   def show
     @blog = Blog.find(params[:id])
     @users = User.all      

         respond_to do |format|
           format.html # show.html.erb
           format.json { render json: @blog }
         end
      end

index.html.erb コードは次のとおりです。

     <% @blogs.each do |blog| %>
       <tr>
        <td><%= blog.title %></td>
        <td><%= blog.post %></td>
        <td><%= blog.user.username %></td> #blog.user_id works perfectly here.
        <td><%= link_to 'Show', blog %></td>
        <td><%= link_to 'Edit', edit_blog_path(blog) %></td>
        <td><%= link_to 'Destroy', blog, method: :delete, data: { confirm:'sure?'} %></td>
       </tr>
         <% end %>

これは、show.html.erb ファイルのコードです。

      <p>
      <b>Title:</b>
       <%= @blog.title %>
       </p>

       <p>
       <b>Author:</b>
        <%= @blog.user.username %>
       </p>

       <p>
       <b>Post:</b>
       <%= @blog.post %>
       </p>

ブログコントローラーの作成コードは次のとおりです。

      Here is the create code:

    # POST /blogs
    # POST /blogs.json
        def create
         @blog = Blog.new(params[:blog])
         @blog.user_id = current_user

           respond_to do |format|
            if @blog.save
              format.html { redirect_to @blog, notice: 'Blog was successfully created.' }
              format.json { render json: @blog, status: :created, location: @blog }
               else
                format.html { render action: "new" }
                format.json { render json: @blog.errors, status: :unprocessable_entity }
              end
            end
          end
4

2 に答える 2

2
class Blog < ActiveRecord::Base
   attr_accessible :post, :title

    #has_one :users // remove this line of code..
    belongs_to :user

end

また、user_id が存在しないユーザー レコードを指している可能性があります。

于 2012-06-22T19:29:20.000 に答える
0

ブログ テーブルを確認し、作成されたすべてのブログに user_id が存在するかどうかを確認してください。

また、必要な関連付けに従ってユーザーモデルが変更されることを確認してください。

1 対 1 の関係を使用している場合、ユーザー モデルには

class User < ActiveRecord::Base
  has_one :blog
end

または 1 対多の関係を使用している場合、ユーザー モデルには

class User < ActiveRecord::Base
  has_many :blogs
end

次のようにブログ モデルを変更します。

class Blog < ActiveRecord::Base
   attr_accessible :post, :title
   belongs_to :user
end

ブログコントローラーに次の変更を加えて、新しいブログを作成してみてください。

def create
 @blog = Blog.new(params[:blog])
 @blog.user_id = current_user.id

 respond_to do |format|
  if @blog.save
   format.html { redirect_to @blog, notice: 'Blog was successfully created.' }
   format.json { render json: @blog, status: :created, location: @blog }
  else
   format.html { render action: "new" }
   format.json { render json: @blog.errors, status: :unprocessable_entity }
  end
 end
end

ユーザーテーブルにユーザー名列がありますか?

于 2013-09-03T08:37:32.007 に答える