我是新手,正在创建一个简单的应用程序,用户可以在其中添加帖子,然后向该帖子添加评论。该模型的代码是
# == 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的内容
请帮助
当您在Ruby类中声明方法时,该方法的最后定义将覆盖以前具有相同方法名称的所有声明。
您有两种create
方法,一种是逻辑方法,第二种是空方法。因为第二个是空的,所以它正在运行,而不是您的所有逻辑所在的第一个。