0

我正在尝试处理评论的更新。现在我很确定这应该是一个 PUT 请求,但是我似乎得到了一个像这样的 GET 命令:

没有路线匹配 [GET] "/books/10/snippets/24"

这是我的片段控制器:

class SnippetsController < ApplicationController
 before_filter :authenticate_user!, only: [:create]
 before_filter :find_book

 def create
    @snippet = @book.snippets.create!(params[:snippet])
    redirect_to @book
  end      


  def edit
    @snippet = @book.snippets.find(params[:id])
  end    

  def update
    @snippet = @book.snippets.find(params[:id])

    respond_to do |format|
      if @snippet.update_attributes(params[:book])
        format.html { redirect_to [@book, @snippet], notice: 'Comment was successfully updated.' }
      else
        format.html { render action: "edit" }
      end
    end
  end

  private

  def find_book
    @book = Book.find(params[:book_id])
  end
end

这是我的表格,首先是 _form.html.erb

<%= form_for([@book, @snippet]) do |f| %>
  <% if @snippet.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@snippet.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>
      <% @snippet.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">

    <%= f.text_field :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

然后我的片段部分,据我了解,它仅用于创建或显示与其父级(书籍)相关的片段。

<%= div_for snippet do %>
        <p>
                <strong>
                        Posted <%= time_ago_in_words(snippet.created_at) %> ago
                </strong>
                <br/>
                <%= snippet.body %>
                <br/>
                <%= link_to 'Edit', edit_book_snippet_path(@book, snippet) %> |
                <%= link_to 'Back', books_path %>
        </p>
<% end %>

这是我从本地主机获得的路线:

> http://localhost:3000/books/10/snippets/24

这是我的耙子路线:

$ rake routes
           book_snippets POST   /books/:book_id/snippets(.:format)          snippets#create
       edit_book_snippet GET    /books/:book_id/snippets/:id/edit(.:format) snippets#edit
            book_snippet PUT    /books/:book_id/snippets/:id(.:format)      snippets#update
                         DELETE /books/:book_id/snippets/:id(.:format)      snippets#destroy
                   books GET    /books(.:format)                            books#index
                         POST   /books(.:format)                            books#create
                new_book GET    /books/new(.:format)                        books#new
               edit_book GET    /books/:id/edit(.:format)                   books#edit
                    book GET    /books/:id(.:format)                        books#show
                         PUT    /books/:id(.:format)                        books#update
                         DELETE /books/:id(.:format)                        books#destroy

Routes.db 文件:

App1::Application.routes.draw do
    resources :books do
      resources :snippets, :only => [:create, :edit, :update, :destroy]
end

  devise_for :admins

  get "profiles/show"

  as :user do
    get '/register', to: 'devise/registrations#new', as: :register
    get '/login', to: 'devise/sessions#new', as: :login
    get '/logout', to: 'devise/sessions#destroy', as: :logout
  end

  devise_for :users, skip: [:sessions]

  as :user do
    get "/login" => 'devise/sessions#new', as: :new_user_session
    post "/login" => 'devise/sessions#create', as: :user_session
    delete "/logout" => 'devise/sessions#destroy', as: :destroy_user_session
  end

  resources :user_friendships do
    member do
      put :accept
    end
  end

  resources :statuses
  get 'feed', to: 'statuses#index', as: :feed
  root to: 'statuses#index'

  get '/:id', to: 'profiles#show', as: 'profile'



  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => 'welcome#index'

  # See how all your routes lay out with "rake routes"

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id))(.:format)'
end

日志 - 终端

ActionController::RoutingError (No route matches [GET] "/books/10/snippets/24"):
  actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.6) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.6) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.6) lib/rails/engine.rb:479:in `call'
  railties (3.2.6) lib/rails/application.rb:220:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

非常感谢您的帮助,或者了解如何更新嵌套评论的想法会很有用。我可以进入编辑阶段,这很好,但它只是从我理解的拖网中提交的表单提交由“def update”处理。

再次感谢你的帮助。

4

2 回答 2

0

由于您在书籍中嵌套了代码片段,因此您还必须嵌套控制器和视图,以便 rails 知道在哪里可以找到它们。例如,控制器 -> 书籍 -> snippets_controller.rb。

于 2013-09-25T08:18:48.160 回答
0

对我来说,片段似乎正在更新,但问题在于重定向,如果

@snippet.update_attributes(params[:book])
        format.html { redirect_to [@book, @snippet], notice: 'Comment was successfully updated.' }

您应该定义 show 方法

def show
end
于 2013-09-25T09:13:41.383 回答