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

ブック内にスニペットをネストしたため、レールがそれらを見つける場所を認識できるように、コントローラーとビューもネストする必要があります。たとえば、コントローラー -> 本 -> snippets_controller.rb です。

于 2013-09-25T08:18:48.160 に答える