数据未保存在rails的数据库中

时间:2015-03-16 17:57:07

标签: ruby-on-rails ruby-on-rails-4.1

我是新的rails并且正在创建一个简单的应用程序,用户可以在其中添加帖子,然后为帖子添加评论。 该模型的代码是

 # == Schema Information
#
# Table name: comments
#
#  id         :integer          not null, primary key
#  issue_id   :integer
#  content    :text
#  created_at :datetime
#  updated_at :datetime
#

class Comment < ActiveRecord::Base



    belongs_to :issue

end

控制器的代码是

class CommentsController < ApplicationController


def create 
    @issue = Issue.find(params[:issue_id])
    @comment = @issue.comments.create(comment_params)

    if @comment.save
        redirect_to :controller => 'issues', :action => 'index'
    else 
        render 'new'
    end
end 



def create
end 



private
    def comment_params
        params.require(:comment).permit(:content)

    end


end

以下关联已添加到发布控制器

has_many :comments

当我在表单中输入数据时,rails不会将数据保存到数据库,而是显示文件的内容comment.html.erb

请帮忙

1 个答案:

答案 0 :(得分:1)

当您在Ruby类中声明方法时,该方法的最后一个定义将覆盖具有相同方法签名(方法名称和参数)的任何先前声明。

你有两个create方法,一个用逻辑,第二个用空。因为第二个是空的,所以它是在运行而不是第一个运行,其中所有的逻辑都是。

相关问题