0

提交我的表单 (_reply_form.html.erb) 时,我收到此错误:

Routing Error

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

这就是我的架构的设置方式:产品有很多响应。响应有很多回复。

我知道我需要表单来提交到这条路线,但我无法做到这一点:

   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} %> 

产品/show.html.erb:

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

响应控制器.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

路线.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 路线产生这个:

               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
4

1 回答 1

2

-- 原始问题

这应该适用于您的form_for

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

-- 你问题的第二部分route matches {:action=>"show", :controller=>"replies"}

我在您的代码中没有看到任何错误,可能是您没有粘贴的代码的一部分?尝试搜索对应的 link_to

- 奖金

此外,您不需要render使用局部变量编写控制器实例变量@response,并且@reply会自动出现在您的局部变量中

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

当然,如果您正在使用responseandreply在您的部分中,您可以将它们分别重命名为@response@reply

于 2013-08-04T00:51:26.203 回答