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

response_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

ルート.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| %>

-- 質問の 2 番目の部分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

もちろん、パーシャルでresponseandを使用している場合は、それらの名前をそれぞれandに変更できますreply@response@reply

于 2013-08-04T00:51:26.203 に答える