当我试图创建一个新的"参与"在我的Rails4应用程序中,我的参数似乎有问题。实际上这并没有在应用程序中引起任何问题(或者我没有注意到它),但我仍然想解决它。你可以看到2" examination_id"参数中的一个为null,另一个等于1.
请求参数:
{"utf8"=>"✓",
"authenticity_token"=>"XZ71eV0zxrnTBilzvEtLlHLwoAb+qKdDfxOHjrAHUPg=",
"participation"=>
{
"language_preference"=>"Türkçe",
"exam_center_preference"=>"1",
"disability"=>"1",
"user_id"=>"1",
"examination_id"=>""
},
"commit"=>"Sınava Başvur",
"examination_id"=>"1"
}
routes.rb中:
resources :examinations do
resources :participations do
member do
get :update_profile_information
end
end
end
participation.rb:
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
before_save :verification_key_generator
end
participations_controller.rb:
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
def index
@participations = @examination.participations
end
def show
@participation = @examination.participations.find(params[:id])
end
def new
@participation = Participation.new
end
def create
@participation = @examination.participations.new(participation_params)
@participation.user = current_user
respond_to do |format|
if @participation.save
format.html { redirect_to [@examination, @participation], notice: 'Başvuru işlemi başarıyla tamamlandı!' }
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
def update
respond_to do |format|
if @participation.update(participation_params)
format.html { redirect_to [@examination, @participation], notice: 'Başvurunuz Başarıyla 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
def set_participation
@participation = Participation.find(params[:id])
end
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
应用/视图/参股/ _form.html.erb:
<%= simple_form_for([@examination, @participation], html:{class: "well"}) do |f| %>
<%= 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: 'Sınav Merkezi Seçiniz', label_method: :city %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.button :submit, "Sınava Başvur" %>
<% end %>
应用/视图/参股/ new.html.erb:
<%= simple_form_for([@examination, @participation]) do |f| %>
<%= f.error_notification %>
<%= 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: 'Sınav Merkezi Seçiniz', label_method: :city %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.button :submit, "Sınava Başvur" %>
<% end %>
SA
答案 0 :(得分:1)
当您使用
时<%= simple_form_for([@examination, @participation], html:{class: "well"}) do |f| %>
要生成表单,它会将操作设置为/examinations/[examination_id]/participations
,以便路由/操作知道网址本身的examination_id
。
因此,您不需要将examination_id
作为您设置为
<%= f.input :examination_id, as: :hidden %>
删除此隐藏字段后,请求参数将如下所示:
{"utf8"=>"✓",
"authenticity_token"=>"XZ71eV0zxrnTBilzvEtLlHLwoAb+qKdDfxOHjrAHUPg=",
"participation"=>
{
"language_preference"=>"Türkçe",
"exam_center_preference"=>"1",
"disability"=>"1",
"user_id"=>"1"
},
"commit"=>"Sınava Başvur",
"examination_id"=>"1"
}