0

I'm trying to process an update on a comment. Now I'm pretty sure this should be a PUT request however I seem to be getting a GET command like so:

No route matches [GET] "/books/10/snippets/24"

Here is my controller for snippets:

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

Here are my forms, firstly _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 %>

Then my snippet partial which from my understanding is only used for creating or showing a snippet in relation to it's parent (books).

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

Here is the route I get from localhost:

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

and here is my rake routes:

$ 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 file:

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

Logs - Terminal

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'

Help is really appreciated or an idea of how to updated nested comments would be useful. I can get to the edit stage which is fine but it is just the form submission which from trawling the net I understand is dealt with by the "def update".

Thanks again for your help.

Shaun Frost Duke Jackson
  • 1,256
  • 1
  • 10
  • 22

2 Answers2

0

Since you've nested snippets within books you will have to nest your controllers and views as well so rails knows where to find them. controllers -> books -> snippets_controller.rb for example.

rdmcfee
  • 540
  • 6
  • 13
0

To me it seems like the snippets are getting updated but the issue is in redirection if

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

You should define the show method

def show
end
techvineet
  • 5,041
  • 2
  • 30
  • 28
  • The thing is this URL will try to take you to show. Instead you should have redirect_to @book and books controller should have show action. – techvineet Sep 25 '13 at 09:22
  • Thanks we I soleved this issue in the update by pointing the redirect to book, the parent and changing the params to book. My book may have many posts so it's best to go back to book rather than show all. – Shaun Frost Duke Jackson Sep 25 '13 at 09:25