是的,我一直在努力解决这个路由问题。
我有一个名为snippet的嵌套评论部分,这个为belongs_to:books
请参阅我的管理员索引,代码段控制器&按顺序排列的路线,我刚刚实施了浅层资源。
这是我得到的当前错误:
无法找到ID = 2
的图书
只有一本书,它有很多片段,但我认为它试图寻找片段ID,而不是关于批准行动的书。我不明白为什么?
<h3>Users</h3>
<div class="span8">
<table class="table table-condensed">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Registered</th>
<th>Role</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= link_to user.full_name, user %></td>
<td><%= user.email %></td>
<td><%= user.created_at.to_date %></td>
<td><%= user.roles.first.name.titleize unless user.roles.first.nil? %></td>
<td>
<a data-toggle="modal" href="#role-options-<%= user.id %>" class="btn btn-mini" type="button">Change role</a>
<%= render user %>
</td>
<td><%= link_to("Delete user", user_path(user), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => 'btn btn-mini') unless user == current_user %></td>
</tr>
<% end %>
</tbody>
</div>
<div class="span8">
<table class="table table-condensed">
<thead>
<tr>
<th>Username</th>
<th>Book Created</th>
<th>Registered</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @books.each do |book| %>
<tr>
<td><%= link_to book.title, book %></td>
<td><%= book.created_at.to_date %></td>
<td><%= render book %></td>
<td>Status</td>
<td><%= button_to 'Approve', active_book_path(book) %></td>
</tr>
</tr>
<% end %>
</tbody>
</div>
<div class="span8">
<table class="table table-condensed">
<thead>
<tr>
<th>Username</th>
<th>Book Created</th>
<th>Registered</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @snippets.each do |snippet| %>
<tr>
<td><%= link_to snippet.body %></td>
<td><%= snippet.created_at.to_date %></td>
<td><%= render snippet %></td>
<td>Status</td>
<td><%= button_to 'Approve', active_snippet_path(snippet) %></td>
</tr>
</tr>
<% end %>
</tbody>
</div>
Snippet_controller
class SnippetsController < ApplicationController
before_filter :authenticate_user!, only: [:create], :except => [:index, :show]
before_filter :find_book
def create
@snippet = @book.snippets.create!(params[:snippet])
redirect_to @book
end
def approve
@snippet = Snippet.find(params[:id])
if @snippet.update_attribute(:approved, true)
redirect_to users_path
else
render root_path
end
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[:snippet])
format.html { redirect_to @book, notice: 'Comment was successfully updated.' }
else
format.html { render action: "edit" }
end
end
end
private
def find_book
@book = Book.find(params[:id])
end
end
路线
Treebook::Application.routes.draw do
root to: 'pages#home'
post "recurly/push"
get "content/Clerk"
get "content/Author"
get "content/Editor"
match '/home' => 'pages#home'
match '/about' => 'pages#about'
match '/contact' => 'pages#contact'
match '/subscribe' => 'pages#subscribe'
match '/new' => 'pages#new'
match "books/:id/activate" => "books#approve", :as => "active_book"
match "/snippets/:id/activate" => "snippets#approve", :as => "active_snippet"
resources :books do
resources :snippets, shallow: true, :only => [:create, :edit, :update, :destroy]
end
devise_for :admins
devise_for :users, :controllers => { :registrations => 'registrations' }
devise_scope :user do
put 'update_plan', :to => 'registrations#update_plan'
end
get "profiles/show"
as :user do
get '/register', to: 'devise/registrations#new', as: :register
get '/register1', to: 'devise/registrations#new2', as: :registernew
get '/login', to: 'devise/sessions#new', as: :login
get '/logout', to: 'devise/sessions#destroy', as: :logout
end
resources :users
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
end
Rake Routes
root / pages#home
recurly_push POST /recurly/push(.:format) recurly#push
content_Clerk GET /content/Clerk(.:format) content#Clerk
content_Author GET /content/Author(.:format) content#Author
content_Editor GET /content/Editor(.:format) content#Editor
home /home(.:format) pages#home
about /about(.:format) pages#about
contact /contact(.:format) pages#contact
subscribe /subscribe(.:format) pages#subscribe
new /new(.:format) pages#new
active_book /books/:id/activate(.:format) books#approve
active_snippet /snippets/:id/activate(.:format) snippets#approve
book_snippets POST /books/:book_id/snippets(.:format) snippets#create
edit_snippet GET /snippets/:id/edit(.:format) snippets#edit
snippet PUT /snippets/:id(.:format) snippets#update
DELETE /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
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy
admin_unlock POST /admins/unlock(.:format) devise/unlocks#create
new_admin_unlock GET /admins/unlock/new(.:format) devise/unlocks#new
GET /admins/unlock(.:format) devise/unlocks#show
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
update_plan PUT /update_plan(.:format) registrations#update_plan
profiles_show GET /profiles/show(.:format) profiles#show
register GET /register(.:format) devise/registrations#new
registernew GET /register1(.:format) devise/registrations#new2
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
accept_user_friendship PUT /user_friendships/:id/accept(.:format) user_friendships#accept
user_friendships GET /user_friendships(.:format) user_friendships#index
POST /user_friendships(.:format) user_friendships#create
new_user_friendship GET /user_friendships/new(.:format) user_friendships#new
edit_user_friendship GET /user_friendships/:id/edit(.:format) user_friendships#edit
user_friendship GET /user_friendships/:id(.:format) user_friendships#show
PUT /user_friendships/:id(.:format) user_friendships#update
DELETE /user_friendships/:id(.:format) user_friendships#destroy
statuses GET /statuses(.:format) statuses#index
POST /statuses(.:format) statuses#create
new_status GET /statuses/new(.:format) statuses#new
edit_status GET /statuses/:id/edit(.:format) statuses#edit
status GET /statuses/:id(.:format) statuses#show
PUT /statuses/:id(.:format) statuses#update
DELETE /statuses/:id(.:format) statuses#destroy
feed GET /feed(.:format) statuses#index
profile GET /:id(.:format) profiles#show
paypal_checkout GET /paypal/checkout(.:format) subscriptions#paypal_checkout
subscriptions GET /subscriptions(.:format) subscriptions#index
POST /subscriptions(.:format) subscriptions#create
new_subscription GET /subscriptions/new(.:format) subscriptions#new
edit_subscription GET /subscriptions/:id/edit(.:format) subscriptions#edit
subscription GET /subscriptions/:id(.:format) subscriptions#show
PUT /subscriptions/:id(.:format) subscriptions#update
DELETE /subscriptions/:id(.:format) subscriptions#destroy
完整错误跟踪
activerecord (3.2.6) lib/active_record/relation/finder_methods.rb:341:in `find_one'
activerecord (3.2.6) lib/active_record/relation/finder_methods.rb:312:in `find_with_ids'
activerecord (3.2.6) lib/active_record/relation/finder_methods.rb:107:in `find'
c:in `find'
app/controllers/snippets_controller.rb:39:in `find_book'
activesupport (3.2.6) lib/active_support/callbacks.rb:451:in `_run__215511428__process_action__130375634__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.6) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.6) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.6) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/params_wrapper.rb:206:in `process_action'
activerecord (3.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.6) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.6) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.6) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:600:in `call'
request_store (1.0.5) lib/request_store/middleware.rb:9:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.6) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `_run__388865935__call__300725136__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:16: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'
答案 0 :(得分:0)
尝试运行get controller#action
,而不是match 'controller#action'
get "books/:id/activate" => "books#approve", :as => "active_book"
get "/snippets/:id/activate" => "snippets#approve", :as => "active_snippet"