I'm having trouble with nested resources in my Rails4 application. What am I missing here?
I can view the participations/new.html.erb form well but when I submit it's returning "No route matches [POST] "/examinations/1-ales/participations/new" error.
When I'm trying to edit a participation for example this url: localhost:3000/examinations/1-ales/participations/15/edit is returning "ActionController::ParameterMissing in ParticipationsController#edit" and "param not found: participation".
- Edit and destroy links in the index.html.erb file causing error:
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]"
routes.rb
resources :examinations do
resources :participations
end
examination.rb
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
participation.rb
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
participations_controller.rb
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
app/views/participations/new.html.erb
<%= 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 %>
app/views/participations/edit.html.erb
<%= 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 %>
app/views/participations/index.html.erb
** Links, which are causing error in the application.
<%= 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' %>