Ruby on rails Rails 4禁止属性错误-嵌套资源
我的Rails 4应用程序中出现了“禁止属性错误”。我错过了什么 还有一个问题,为什么“检查id”参数没有发送到请求 请求Ruby on rails Rails 4禁止属性错误-嵌套资源,ruby-on-rails,ruby,Ruby On Rails,Ruby,我的Rails 4应用程序中出现了“禁止属性错误”。我错过了什么 还有一个问题,为什么“检查id”参数没有发送到请求 请求 Started POST "/examinations/1/participations" for 127.0.0.1 at 2014-03-26 10:47:01 +0200 Processing by ParticipationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_
Started POST "/examinations/1/participations" for 127.0.0.1 at 2014-03-26 10:47:01 +0200
Processing by ParticipationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"EuGZIXKJE9a1It6Ema5t+g07vXngQoqPMV5qQBfekfg=", "participation"=>{"user_id"=>"1", "examination_id"=>"", "language_preference"=>"İngilizce", "exam_center_preference"=>"1", "disability"=>"0"}, "commit"=>"Sınava Başvur", "examination_id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Examination Load (0.2ms) SELECT "examinations".* FROM "examinations" WHERE "examinations"."id" = ? LIMIT 1 [["id", "1"]]
Completed 500 Internal Server Error in 5ms
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
app/controllers/participations_controller.rb:37:in `create'
Routes.rb
resources :examinations do
resources :participations
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
has_many :exam_fees, dependent: :destroy
has_many :exam_languages, dependent: :destroy
end
#encoding: utf-8
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
before_filter :get_examination
def get_examination
@examination = Examination.find(params[:examination_id])
end
# GET /participations
# GET /participations.json
def index
@participations = @examination.participations
end
# GET /participations/1
# GET /participations/1.json
def show
@participation = @examination.participations.find(params[:id])
end
# GET /participations/new
def new
@participation = Participation.new
end
# GET /participations/1/edit
def edit
end
# POST /participations
# POST /participations.json
def create
@participation = @examination.participations.new(params[:participation])
@participation.user = current_user
respond_to do |format|
if @participation.save
redirect_to @examination
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımınız Oluşturuldu!' }
format.json { render action: 'show', status: :created, location: [@examination, @participation] }
else
render 'new'
format.html { render action: 'new' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /participations/1
# PATCH/PUT /participations/1.json
def update
respond_to do |format|
if @participation.update(participation_params)
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımını Güncellendi!' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_participation
@participation = Participation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
<%= simple_form_for([@examination, @participation]) do |f| %>
<%= f.error_notification %>
<fieldset>
<legend>Sınav Katılımı</legend>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.input :language_preference, collection: ["Türkçe", "İngilizce", "Rusça"], label: 'Sınav Dili Tercihi' %>
<%= f.input :exam_center_preference, collection:ExamCenter.all, label_method: :city, as: :select, label: 'Sınav Merkezi Tercihi' %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.button :submit, "Sınava Başvur" %>
</fieldset>
<% end %>
参与。rb
resources :examinations do
resources :participations
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
has_many :exam_fees, dependent: :destroy
has_many :exam_languages, dependent: :destroy
end
#encoding: utf-8
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
before_filter :get_examination
def get_examination
@examination = Examination.find(params[:examination_id])
end
# GET /participations
# GET /participations.json
def index
@participations = @examination.participations
end
# GET /participations/1
# GET /participations/1.json
def show
@participation = @examination.participations.find(params[:id])
end
# GET /participations/new
def new
@participation = Participation.new
end
# GET /participations/1/edit
def edit
end
# POST /participations
# POST /participations.json
def create
@participation = @examination.participations.new(params[:participation])
@participation.user = current_user
respond_to do |format|
if @participation.save
redirect_to @examination
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımınız Oluşturuldu!' }
format.json { render action: 'show', status: :created, location: [@examination, @participation] }
else
render 'new'
format.html { render action: 'new' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /participations/1
# PATCH/PUT /participations/1.json
def update
respond_to do |format|
if @participation.update(participation_params)
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımını Güncellendi!' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_participation
@participation = Participation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
<%= simple_form_for([@examination, @participation]) do |f| %>
<%= f.error_notification %>
<fieldset>
<legend>Sınav Katılımı</legend>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.input :language_preference, collection: ["Türkçe", "İngilizce", "Rusça"], label: 'Sınav Dili Tercihi' %>
<%= f.input :exam_center_preference, collection:ExamCenter.all, label_method: :city, as: :select, label: 'Sınav Merkezi Tercihi' %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.button :submit, "Sınava Başvur" %>
</fieldset>
<% end %>
课堂参与
考试.rb
resources :examinations do
resources :participations
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
has_many :exam_fees, dependent: :destroy
has_many :exam_languages, dependent: :destroy
end
#encoding: utf-8
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
before_filter :get_examination
def get_examination
@examination = Examination.find(params[:examination_id])
end
# GET /participations
# GET /participations.json
def index
@participations = @examination.participations
end
# GET /participations/1
# GET /participations/1.json
def show
@participation = @examination.participations.find(params[:id])
end
# GET /participations/new
def new
@participation = Participation.new
end
# GET /participations/1/edit
def edit
end
# POST /participations
# POST /participations.json
def create
@participation = @examination.participations.new(params[:participation])
@participation.user = current_user
respond_to do |format|
if @participation.save
redirect_to @examination
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımınız Oluşturuldu!' }
format.json { render action: 'show', status: :created, location: [@examination, @participation] }
else
render 'new'
format.html { render action: 'new' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /participations/1
# PATCH/PUT /participations/1.json
def update
respond_to do |format|
if @participation.update(participation_params)
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımını Güncellendi!' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_participation
@participation = Participation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
<%= simple_form_for([@examination, @participation]) do |f| %>
<%= f.error_notification %>
<fieldset>
<legend>Sınav Katılımı</legend>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.input :language_preference, collection: ["Türkçe", "İngilizce", "Rusça"], label: 'Sınav Dili Tercihi' %>
<%= f.input :exam_center_preference, collection:ExamCenter.all, label_method: :city, as: :select, label: 'Sınav Merkezi Tercihi' %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.button :submit, "Sınava Başvur" %>
</fieldset>
<% end %>
课堂考试:参与
有很多:考试费,考试费
有很多:考试语言,依赖::销毁
结束
参与\u controller.rb
resources :examinations do
resources :participations
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
has_many :exam_fees, dependent: :destroy
has_many :exam_languages, dependent: :destroy
end
#encoding: utf-8
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
before_filter :get_examination
def get_examination
@examination = Examination.find(params[:examination_id])
end
# GET /participations
# GET /participations.json
def index
@participations = @examination.participations
end
# GET /participations/1
# GET /participations/1.json
def show
@participation = @examination.participations.find(params[:id])
end
# GET /participations/new
def new
@participation = Participation.new
end
# GET /participations/1/edit
def edit
end
# POST /participations
# POST /participations.json
def create
@participation = @examination.participations.new(params[:participation])
@participation.user = current_user
respond_to do |format|
if @participation.save
redirect_to @examination
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımınız Oluşturuldu!' }
format.json { render action: 'show', status: :created, location: [@examination, @participation] }
else
render 'new'
format.html { render action: 'new' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /participations/1
# PATCH/PUT /participations/1.json
def update
respond_to do |format|
if @participation.update(participation_params)
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımını Güncellendi!' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_participation
@participation = Participation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
<%= simple_form_for([@examination, @participation]) do |f| %>
<%= f.error_notification %>
<fieldset>
<legend>Sınav Katılımı</legend>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.input :language_preference, collection: ["Türkçe", "İngilizce", "Rusça"], label: 'Sınav Dili Tercihi' %>
<%= f.input :exam_center_preference, collection:ExamCenter.all, label_method: :city, as: :select, label: 'Sınav Merkezi Tercihi' %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.button :submit, "Sınava Başvur" %>
</fieldset>
<% end %>
编码:utf-8
类参与控制器<应用程序控制器
过滤前:验证用户身份!
在\u操作之前:设置\u参与,仅:[:显示,:编辑,:更新,:销毁]
过滤前:获取检查
def检查
@检查=检查.查找(参数[:检查id])
结束
#获得/参与
#GET/participations.json
def索引
@参与=@examition.participations
结束
#获取/参与/1
#GET/participations/1.json
def秀
@参与=@examition.participations.find(参数[:id])
结束
#获取/参与/新建
def新
@参与=参与
结束
#获取/参与/1/编辑
定义编辑
结束
#职位/参与
#POST/participations.json
def创建
@参与=@examition.participations.new(参数[:参与])
@participation.user=当前用户
回应待办事项|格式|
如果@participation.save
将_重定向到@检查
format.html{重定向到[@examition,@participation],注意:'Sınav Katılımınız Oluşturuldu!'
format.json{呈现操作:'show',状态::已创建,位置:[@examice,@participation]}
其他的
呈现“新”
format.html{呈现操作:'new'}
format.json{render json:@participation.errors,status::unprocessable_entity}
结束
结束
结束
#修补/放置/参与/1
#PATCH/PUT/participations/1.json
def更新
回应待办事项|格式|
if@participation.update(参与参数)
format.html{重定向到[@examition,@participation],注意:'Sınav KatılımınıGüncellendi!'
format.json{head:no_content}
其他的
format.html{呈现操作:“编辑”}
format.json{render json:@participation.errors,status::unprocessable_entity}
结束
结束
结束
私有的
#使用回调在操作之间共享公共设置或约束。
def set_参与
@参与=参与。查找(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def参与参数
参数要求(:参与)。许可(:用户id、:考试id、:付款状态、:语言偏好、:考试中心偏好、:残疾)
结束
结束
/views/participations/_-form.html.erb
resources :examinations do
resources :participations
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
has_many :exam_fees, dependent: :destroy
has_many :exam_languages, dependent: :destroy
end
#encoding: utf-8
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
before_filter :get_examination
def get_examination
@examination = Examination.find(params[:examination_id])
end
# GET /participations
# GET /participations.json
def index
@participations = @examination.participations
end
# GET /participations/1
# GET /participations/1.json
def show
@participation = @examination.participations.find(params[:id])
end
# GET /participations/new
def new
@participation = Participation.new
end
# GET /participations/1/edit
def edit
end
# POST /participations
# POST /participations.json
def create
@participation = @examination.participations.new(params[:participation])
@participation.user = current_user
respond_to do |format|
if @participation.save
redirect_to @examination
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımınız Oluşturuldu!' }
format.json { render action: 'show', status: :created, location: [@examination, @participation] }
else
render 'new'
format.html { render action: 'new' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /participations/1
# PATCH/PUT /participations/1.json
def update
respond_to do |format|
if @participation.update(participation_params)
format.html { redirect_to [@examination, @participation], notice: 'Sınav Katılımını Güncellendi!' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_participation
@participation = Participation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
<%= simple_form_for([@examination, @participation]) do |f| %>
<%= f.error_notification %>
<fieldset>
<legend>Sınav Katılımı</legend>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.input :language_preference, collection: ["Türkçe", "İngilizce", "Rusça"], label: 'Sınav Dili Tercihi' %>
<%= f.input :exam_center_preference, collection:ExamCenter.all, label_method: :city, as: :select, label: 'Sınav Merkezi Tercihi' %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.button :submit, "Sınava Başvur" %>
</fieldset>
<% end %>
Sınav Katılımı
:hidden,:input_html=>{:value=>current_user.id}%>
为了将Rails 4中的参数分配给对象,您应该使用在participation\u params
方法中实现的强参数“语法”,而不是直接传递参数。因此,改变路线:
@participation = @examination.participations.new(params[:participation])
致:
由于您通过关联创建了您的
参与
记录,因此在此控制器中实际上不需要检查id
参数。更重要的是,如果您允许此参数,那么将参与
分配给检查
将变得很容易,而不是从您创建的参与
上下文中分配,我怀疑这是不可取的。因此,我想您应该从表单中的字段和参与参数中删除检查id
您需要向我们展示您拥有的任何表格代码,以便向您展示为什么您的检查id
被发送到参与
部分之外hash@RichPeck我编辑了我的问题并添加了表格partial@msdundar你试过我的解决方案吗?这么好的答案!谢谢,谢谢,谢谢。