rails 中的数据未保存在数据库中
data is not saved in database in rails
我是新手 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
的内容
请帮忙
当您在 Ruby class 中声明方法时,方法的最后一个定义将覆盖具有相同方法名称的任何先前声明。
您有两个 create
方法,一个有逻辑,另一个是空的。因为第二个是空的,所以它是 运行 而不是第一个,你所有的逻辑都是。
我是新手 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
的内容请帮忙
当您在 Ruby class 中声明方法时,方法的最后一个定义将覆盖具有相同方法名称的任何先前声明。
您有两个 create
方法,一个有逻辑,另一个是空的。因为第二个是空的,所以它是 运行 而不是第一个,你所有的逻辑都是。