我在Rails4应用程序中遇到嵌套资源的问题。我在这里缺少什么?
我可以很好地查看参与/ new.html.erb表单但是当我提交它时返回“没有路由匹配[POST]”/ examinations / 1-ales / participations / new“错误。
当我尝试编辑参与时,例如这个url:localhost:3000 / examinations / 1-ales / participations / 15 / edit正在返回“参与控件中的ActionController :: ParameterMissing#edit”和“param”没找到:参与“。
ActionController::UrlGenerationError in Participations#index
No route matches {:action=>"edit", :controller=>"participations",
:examination_id=>#<Participation id: 12, user_id: 1, examination_id: 1,
payment_status: false, language_preference: "English">,
id=>nil, :format=>nil} missing required keys: [:id]"
resources :examinations do
resources :participations
end
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
def index
@participations = Participation.all
end
def show
end
def new
@participation = Participation.new
@examination = params[:examination_id]
end
def edit
@examination = params[:examination_id]
@participation = Participation.new(participation_params)
end
def create
@participation = Participation.new(participation_params)
@participation.user = current_user
respond_to do |format|
if @participation.save
redirect_to(@examination)
format.html { redirect_to @participation, notice: 'Sınav Katılımınız Oluşturuldu!' }
else
render 'new'
format.html { render action: 'new' }
end
end
end
def update
respond_to do |format|
if @participation.update(participation_params)
format.html { redirect_to @participation, notice: 'Participation was successfully updated.' }
else
format.html { render action: 'edit' }
end
end
end
def destroy
@participation.destroy
respond_to do |format|
format.html { redirect_to participations_url }
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
<%= simple_form_for @participation, as: :participation, url: new_examination_participation_path(@examination) 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: ["English", "German"] %>
<%= f.button :submit, "Register to Exam" %>
<% end %>
<%= simple_form_for @participation, as: :participation, url: edit_examination_participation_path(@examination) 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: ["English", "German"] %>
<%= f.button :submit, "Register to Exam" %>
<% end %>
**链接,导致应用程序出错。
<%= link_to 'Edit', edit_examination_participation_path(participation), :class => 'btn btn-small' %>
<%= link_to 'Delete', participation, method: :delete, data: { confirm: 'bla bla' }, :class => 'btn btn-small btn-danger' %>
答案 0 :(得分:1)
创建参与:
在app/views/participations/new.html.erb
,
替换
<%= simple_form_for @participation, as: :participation, url: new_examination_participation_path(@examination) do |f| %>
使用
<%= simple_form_for @participation, as: :participation, url: examination_participations_path(@examination, @participation) do |f| %>
examination_participations_path
会在提交表单时将请求定向到ParticipationsController #create
。这就是我认为你想要实现的目标=&gt; Create a Participation
更新参与
在app/views/participations/edit.html.erb
,
替换
<%= simple_form_for @participation, as: :participation, url: edit_examination_participation_path(@examination) do |f| %>
使用
<%= simple_form_for @participation, as: :participation, url: examination_participation_path(@examination, @participation) do |f| %>
examination_participation_path
会在提交表单时将请求定向到ParticipationsController #update
。
列出所有参与:
在app/views/participations/index.html.erb
,
替换
<%= link_to 'Edit', edit_examination_participation_path(participation), :class => 'btn btn-small' %>
<%= link_to 'Delete', participation, method: :delete, data: { confirm: 'bla bla' }, :class => 'btn btn-small btn-danger' %>
使用
<%= link_to 'Edit', edit_examination_participation_path(@examination, participation), :class => 'btn btn-small' %>
<%= link_to 'Delete', examination_participation_path(@examination, participation), method: :delete, data: { confirm: 'bla bla' }, :class => 'btn btn-small btn-danger' %>
注意:我强烈建议您阅读Rails指南中的Nested Resources。
您始终可以执行rake routes
并查看可供您使用的路线列表。