0

When submitting my form (_reply_form.html.erb) I get this error:

Routing Error

No route matches [POST] "/responses/replies/4"
Try running rake routes for more information on available routes.

This is how my architecture is set up: Offering has many Responses. Response has many Replies.

I know I need the form to submit to this route, but I can't get it to do that:

   response_replies POST   /responses/:response_id/replies(.:format)     replies#create

_reply_form.html.erb:

<%= form_for(@reply, :url => response_reply_path([@response, @reply])) do |f| %>

    <%= render 'common/form_errors', object: @reply %> 

    <%= f.label :body, "Your Reply" %>
    <%= f.text_area(:body, :rows => 10, :class => "field span6") %>

    <%= f.submit "Post Reply", :class => "block"  %>    

<% end %>

_response.html.erb:

<%= render 'replies/reply_form', {:response => @response, :reply => @reply} %> 

offerings/show.html.erb:

<% if @offering.responses.any? %>
    <%= render @offering.responses %>
<% else %>
    <p>This offering has no responses yet.</p>
<% end %>

responses_controller.rb:

class ResponsesController < ApplicationController
    before_filter :auth, only: [:create]

    def show
        @offering = Offering.new
        @response = Response.new
        @reply = Reply.new

    end

    def create

        @offering = Offering.find(params[:offering_id])
        # now that we have our offering we use it to 
        # build a response with it
        @response = @offering.responses.build(params[:response])

        # now we get the user who posted the response
        @response.user = current_user

        if @response.save
            flash[:success] = 'your response has been posted!'
            redirect_to @offering
        else
            @offering = Offering.find(params[:offering_id])
            render 'offerings/show'
        end

    end
end

routes.rb:

  resources :offerings, except: [:new] do
    # makes it easier for us to display
    # forms for responses on the offering show page
    # allows us to have access to the  
    # offering that the response is associated to
    resources :responses, only: [:create]
  end

  resources :responses, except: [:new] do
    resources :replies, only: [:create]
  end

rake routes produces this:

               root        /                                             dashboard#index
              users POST   /users(.:format)                              users#create
           new_user GET    /users/new(.:format)                          users#new
           sessions POST   /sessions(.:format)                           sessions#create
        new_session GET    /sessions/new(.:format)                       sessions#new
    need_applicants GET    /needs/:need_id/applicants(.:format)          applicants#index
                    POST   /needs/:need_id/applicants(.:format)          applicants#create
 new_need_applicant GET    /needs/:need_id/applicants/new(.:format)      applicants#new
edit_need_applicant GET    /needs/:need_id/applicants/:id/edit(.:format) applicants#edit
     need_applicant GET    /needs/:need_id/applicants/:id(.:format)      applicants#show
                    PUT    /needs/:need_id/applicants/:id(.:format)      applicants#update
                    DELETE /needs/:need_id/applicants/:id(.:format)      applicants#destroy
              needs GET    /needs(.:format)                              needs#index
                    POST   /needs(.:format)                              needs#create
          edit_need GET    /needs/:id/edit(.:format)                     needs#edit
               need GET    /needs/:id(.:format)                          needs#show
                    PUT    /needs/:id(.:format)                          needs#update
                    DELETE /needs/:id(.:format)                          needs#destroy
 offering_responses POST   /offerings/:offering_id/responses(.:format)   responses#create
          offerings GET    /offerings(.:format)                          offerings#index
                    POST   /offerings(.:format)                          offerings#create
      edit_offering GET    /offerings/:id/edit(.:format)                 offerings#edit
           offering GET    /offerings/:id(.:format)                      offerings#show
                    PUT    /offerings/:id(.:format)                      offerings#update
                    DELETE /offerings/:id(.:format)                      offerings#destroy
   response_replies POST   /responses/:response_id/replies(.:format)     replies#create
          responses GET    /responses(.:format)                          responses#index
                    POST   /responses(.:format)                          responses#create
      edit_response GET    /responses/:id/edit(.:format)                 responses#edit
           response GET    /responses/:id(.:format)                      responses#show
                    PUT    /responses/:id(.:format)                      responses#update
                    DELETE /responses/:id(.:format)                      responses#destroy
           register        /register(.:format)                           users#new
              login        /login(.:format)                              sessions#new
                           /offerings(.:format)                          offerings#index
                           /needs(.:format)                              needs#index
          dashboard        /dashboard(.:format)                          dashboard#index
            contact        /contact(.:format)                            contact#index
     your_offerings        /your_offerings(.:format)                     offerings#your_offerings
         your_needs        /your_needs(.:format)                         needs#your_needs
             search        /search(.:format)                             offerings#search
             logout DELETE /logout(.:format)                             sessions#destroy
Pavan Katepalli
  • 2,372
  • 4
  • 29
  • 52

1 Answers1

2

-- Original question

This should work for your form_for

<%= form_for([@response, @reply], :url => response_reply_path do |f| %>

-- Second part of your question route matches {:action=>"show", :controller=>"replies"}

I don't see anything wrong in your code, it's maybe in a part of your code you did not paste? Try to search for a link_to that would be corresponding

-- Bonus

Also you should not need to write your render with local variables, the controller instance variables @response and @reply will automatically be present in your partial

<%= render 'replies/reply_form' %> 
# @response and @reply are automatically forwarded to all your views and partials

Off course if you are using response and reply in your partial you can rename them to @response and @reply respectively

Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
  • Thanks so much for your help. I did what you mentioned, but when I go to localhost:3000/offerings/4 I get No route matches {:action=>"show", :controller=>"replies"} – Pavan Katepalli Aug 04 '13 at 01:14
  • Hey Ben I did this and it worked: <%= form_for(reply, :url => response_replies_path(response.id)) do |f| %> – Pavan Katepalli Aug 15 '13 at 01:26
  • I got undefined local variable or method `response_reply_path' for #<#:0xb4a47bbc> when I tried out <%= form_for([@response, @reply], :url => response_reply_path) do |f| %> – Pavan Katepalli Aug 15 '13 at 01:30