Ruby on rails 未保存Rails表单参数
我对RubyonRails非常陌生,我正在努力使用我的脚手架控制器。 我制作了一个嵌套资源,将我的评论放在帖子中Ruby on rails 未保存Rails表单参数,ruby-on-rails,ruby-on-rails-4,scaffolding,Ruby On Rails,Ruby On Rails 4,Scaffolding,我对RubyonRails非常陌生,我正在努力使用我的脚手架控制器。 我制作了一个嵌套资源,将我的评论放在帖子中 class Post < ActiveRecord::Base validates :name, :presence => true validates :title, :presence => true, :length => { :minimum => 5 } has_many :comments end class Comment
class Post < ActiveRecord::Base
validates :name, :presence => true
validates :title, :presence => true, :length => { :minimum => 5 }
has_many :comments
end
class Comment < ActiveRecord::Base
validates :commenter, :presence => true
validates :body, :presence => true
belongs_to :post
end
class Posttrue
验证:title,:presence=>true,:length=>{:minimum=>5}
有很多评论
终止
类注释true
验证:body,:presence=>true
属于:职位
终止
控制器的简化版本如下所示
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
# omited new, index, show...
# POST /comments
# POST /comments.json
def create
post = Post.find(params[:post_id])
@comment = post.comments.create(params[:comment].permit(:name, :title, :context))
respond_to do |format|
if @comment.save
format.html { redirect_to([@comment.post, @comment], notice: 'Comment was successfully created.') }
format.json { render action: 'show', status: :created, location: @comment }
else
format.html { render action: 'new' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
post = Post.find(params[:post_id])
@comment = post.comments.find(params[:comment])
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:commenter, :body, :post)
end
end
class CommentsController
当我填写表格时,我得到以下例外情况:
2个错误禁止保存此评论:
评论员不能为空
身体不能是空白的
我尝试了guide,但我认为它与Rails 4不完全兼容。似乎您明确地允许在
创建操作中使用不同的属性集
您应该更新create
操作以使用comment\u参数
,就像您在update
操作中所做的那样。你的评论
的原因肯定是期待评论人
和正文
,而不是:名称、标题、上下文
,这是你在创建
操作中允许的
将控制器的创建操作更新为:
# POST /comments
# POST /comments.json
def create
...
@comment = post.comments.create(comment_params)
...
end
您正在读取帖子的参数属性(:name,:title,:context
),但您需要读取评论
属性(:commenter,:body
)
替换此项:
@comment=post.comments.create(参数[:comment].permit(:name,:title,:context))
与
@comment=post.comments.create(参数[:comment].permit(:commenter,:body))
或者,更好的方法是:
@comment=post.comments.create(comment#u params)
这不起作用:ActiveRecord::AssociationTypeMismatch in CommentsController#create post(#35862540)应为,得到字符串(#1566868)当我在comment中更改参数时,它也起作用。所以也有很多thx!