rails嵌套资源表单的问题

时间:2013-08-03 19:09:36

标签: ruby-on-rails forms nested-resources

出于某种原因,我无法让它发挥作用。

提供有许多回复,而回复有很多回复

我正在尝试在_response.html.erb中显示回复表单(见下文),当我转到http://localhost:3000/offerings/4时,它会抛出此错误:

ActionController::RoutingError at /offerings/4
No route matches {:action=>"create", :controller=>"replies", :offering_id=>#<Offering id: 4, user_id: 1, description: "help you fish ", created_at: "2013-07-30 02:59:38", updated_at: "2013-07-30 02:59:38">, :response_id=>#<Response id: nil, user_id: nil, offering_id: nil, body: nil, created_at: nil, updated_at: nil>, :format=>#<Reply id: nil, user_id: nil, response_id: nil, body: nil, created_at: nil, updated_at: nil>}

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_response_replies POST   /offerings/:offering_id/responses/:response_id/replies(.:format) replies#create
       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
                 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

routes.rb的一部分:

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

_response.html.erb:

<% 

if !logged_in?
    user_id = 'not logged in' 
else 
    user_id = current_user.id 
end

# check whether the response is by  
# the person who posted this offering
# or by the person currently logged in
# otherwise don't show the response
if response.user_id == user_id or @offering.user_id == response.user_id %>

    <li class="well">
        <%= response.body %> - by <%= response.user.username.capitalize %>
    </li>

    <div id="post-reply">
        <h2>Reply to your Response</h2>

        <% if logged_in? %>
            <%= render 'replies/reply_form' %> 
        <% end %>
    </div>

<% end %>

_reply_form.html.erb:

<%= form_for(offering_response_replies_path(@offering, @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 %>

replies_controller.rb:

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

    def create

        @offering = Offering.find(params[:offering_id])

        @response = @offering.responses.build(params[:response])

        @reply = @response.replies.build(params[:reply])

        @reply.user = current_user

        if @reply.save
            flash[:success] = 'your reply has been posted!'
            redirect_to @offering
        else

            @offering = Offering.find(params[:offering_id])
            render 'offerings/show'
        end

    end
end

0 个答案:

没有答案