0
votes

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' %>
1
when you are posting a question about routes, make sure you add your output of rake routesLenin Raj Rajasekaran

1 Answers

1
votes

Create a Participation:

In app/views/participations/new.html.erb,

Replace

<%= simple_form_for @participation, as: :participation, url: new_examination_participation_path(@examination) do |f| %>

With

<%= simple_form_for @participation, as: :participation, url: examination_participations_path(@examination, @participation) do |f| %>

examination_participations_path would direct the the request to ParticipationsController #create upon form submission. That is what I think you are trying to achieve => Create a Participation

Update a Participation:

In app/views/participations/edit.html.erb,

Replace

<%= simple_form_for @participation, as: :participation, url: edit_examination_participation_path(@examination) do |f| %>

With

<%= simple_form_for @participation, as: :participation, url: examination_participation_path(@examination, @participation) do |f| %>

examination_participation_path would direct the the request to ParticipationsController #update upon form submission.

List all Participations:

In app/views/participations/index.html.erb,

Replace

<%= 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' %>

With

<%= 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' %>

NOTE: I would highly recommend you to read about Nested Resources in Rails guides. You can always do rake routes and see the list of routes that are available to you.