2012-11-18 155 views
0

我需要一些帮助,在现有模型中创建一个非常简单的论坛。红宝石轨道2级模型

我想要在游戏页面中拥有一个迷你论坛,可以在其中创建一些主题和对该主题的一些评论。一开始我只是实现主题。

这是错误我:

Mysql2::Error: Column 'user_id' cannot be null: INSERT INTO `topics` (`game_id`, `question`, `user_id`) VALUES (1, 'asd', NULL) 

这是我的主要型号:

game.rb

class Game < ActiveRecord::Base 
    attr_accessible :name 

    validates :user_id, presence: true 
    validates :name, presence: true, length: { maximum: 50 } 

    belongs_to :user 

    has_many :topics, dependent: :destroy 

end 

topic.rb

class Topic < ActiveRecord::Base 
    validates_presence_of :question 
    validates_presence_of :game_id 

    attr_accessible :question, :user_id 

    validates :question, length: {maximum: 50}, allow_blank: false 

    belongs_to :game 
    belongs_to :user 
end 

topic_controller.rb

def create 
    @game = Game.find(params[:game_id]) 
    @topic = @game.topics.create(params[:topic]) 
    @topic.user_id = current_user.id 

    respond_to do |format| 
     if @topic.save 
     format.html { redirect_to @game, notice: 'Topic was successfully created.' } 
     else 
     format.html { render action: "new" } 
     end 
    end 
end 

游戏/ show.html.erb

<h2>Topics</h2> 
<% @game.topics.each do |topic| %> 

    <p> 
     <b>Question:</b> 
     <%= topic.question %> 
    </p> 
<% end %> 

<h2>Add a topic:</h2> 
<%= form_for([@game, @game.topics.build]) do |f| %> 
    <div class="field"> 
    <%= f.label :question %><br /> 
     <%= f.text_field :question %> 
    </div> 
    <div class="actions"> 
     <%= f.submit %> 
    </div> 
<% end %> 

感谢;)

+0

由于您的问题很容易通过调试解决 - 只需使用[debugger](https://github.com/cldwalker/debugger)在这种简单情况下找到问题即可。 –

回答

1

我相信您遇到的问题是Rails中createnew之间的差异。

使用new只是初始化模型,允许您稍后保存/验证;使用create将在一个命令中执行所有这些步骤,从而导致创建数据库行。

所以,当你尝试执行

@game.topics.create(params[:topic]) 

的Rails试图创建使用params[:topic]和设置game_id@game.id一个主题后,它立即试图验证这一新的主题,它创建并保存到数据库。

潜在的选择,你可以考虑:

1)使用@game.topics.new(params[:topic])

2)合并在{:user_id => current_user.id}@game.topics.create(params[:topic].merge({:user_id => current_user.id})

我个人建议选择1(即使用新的来代替),但我已经看到之前使用过的选项2。

编辑:另一个问题,它看起来好像你可能会遇到:应current_user在你的代码@current_user


旁注: 通常,如果create无法创建数据库行,它仍然可以工作(返回初始模型代替),但在你的情况下,它看起来像这种情况不会发生由于数据库级别对user_id的限制不为空,导致未被捕获的错误。

+0

谢谢,这就是问题:D – jony17

0

我猜你是访问该网站而没有登录,所以user_id将不会被设置。您应该确保所有正在修改或创建主题的操作都有登录用户。一个简单的方法可以在this Railscast中找到。

+0

感谢您的回答! 在一开始,我认为这是问题,但不,我登录。 – jony17

0

我觉得current_user.id没有设置正确,请检查这些问题,几乎所有的其他问题,红宝石调试器是围攻的方式

在创业板上市文件中添加

GEM文件

gem 'debugger' 

运行bundle install

然后在你的控制器中

def create 
    @game = Game.find(params[:game_id]) 
    @topic = @game.topics.create(params[:topic]) 

    @topic.user_id = current_user.id 

    debugger 

    respond_to do |format| 
     if @topic.save 
     format.html { redirect_to @game, notice: 'Topic was successfully created.' } 
     else 
     format.html { render action: "new" } 
     end 
    end 
end 

这会停止你在调试器行,然后从控制台,你可以看到如果值设置或不。 check this for more

+0

感谢您的回答,但我已经用@DRobinson答案解决了问题;) – jony17