instead of Post.new I typed User.new, SOLVED!!!
Can't mass-assign protected attributes: even if attr_accessible already added
-
06-03-2022 - |
Question
Can't mass assign :title, :url and :about even if attr_accessible attributes already added. It's fine on rails console but not on online form.
Post Model:
class Post < ActiveRecord::Base
attr_accessible :about, :downv, :names, :points, :title, :upv, :url, :user_id
belongs_to :user
end
User Model:
class User < ActiveRecord::Base
attr_accessible :email, :password_digest, :post_id, :password, :password_confirmation, :name
has_many :posts
has_secure_password
validates_presence_of :password, :on => :create
end
Post Controller create:
def create
@post = User.new(params[:post])
@post.upv, @post.downv, @post.points = 0, 0, 0
@post.user_id = params[:user_id]
@post.names = ""
if @post.save
redirect_to root_url, notice: "Post created."
else
render "new"
end
end
My form view is is just like any other form view.
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow