需要建议
我正在使用rails 4和ruby 1.9.3
我正在尝试在管理员上创建帖子,并对标题和内容的存在进行验证。但是当我创建帖子时,验证错误会引发,即使我填写标题和内容。
这是我的代码
post.rb
class Post < ActiveRecord::Base
validates :title, :presence => true
validates :content, :presence => true
validates :user_id, :presence => true
...
posts_controller.rb
module Admin
class PostsController < ::ApplicationController
layout 'admin'
before_action :set_post, only: [:show, :edit, :update, :destroy, :publish, :unpublish]
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
@post.user = current_user
respond_to do |format|
if @post.save
format.html { redirect_to admin_post_path(@post), notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: @post }
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :content, :published)
end
end
end
并在我的日志中引发警告:无法为Post:title,content。
大量分配受保护的属性感谢的