2012-06-22 110 views
0

我是RoR开发的相对业余爱好者(大约一个月的经验),并且我一直在引用来自不同控制器的变量,直到此时没有问题。我正在为博客开发CMS,并且无法让博客作者的用户名出现在索引或显示页面上。我收到一个“未定义的方法”用户名为“nil:NilClass”错误。但是,我可以获得ID。任何人都可以帮助指出我的代码中的错误?我将不胜感激。基本的Ruby on Rails未定义的方法问题

这是博客模式:

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

    has_one :users 
    belongs_to :user 

     end 

这是博客控制器的表演和指数部分:

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 
+0

你的'has_one:users'不应该是单数吗?换句话说,班博客应该有'has_one:user'。 – Zajn

回答

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

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

end 

还要检查,可能是user_id是指向不存在的用户记录..

0

请检查您的博客表,并查看每个创建的博客中是否存在user_id。

同时检查您的用户模型是否根据您所需的关联进行更改。

如果您正在使用一个一对一的关系那么你的用户模型应该有

class User < ActiveRecord::Base 
    has_one :blog 
end 

或者如果您正在使用一个一对多的关系,则用户模型应具有

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 

请问您的用户表有username列?

相关问题