无法获得表格提交到正确的路线

时间:2013-08-04 00:28:33

标签: ruby-on-rails form-for nested-routes

提交表单时(_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 %>

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路由产生了这个:

               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

1 个答案:

答案 0 :(得分: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

当然,如果您在部分中使用responsereply,则可以将其重命名为@response@reply