Wednesday, 2 October 2013

Admin Control / Route Issues

Admin Control / Route Issues

Right, I've been really struggling with this routing issues.
I have a nested comment section called snippet and this belongs_to :books
Please see my admin index, snippet controller & routes in that order, I've
just implemented shallow resources.
This is the current error I'm getting:
Couldn't find Book with id=2
There is only one book and it has many snippets but I think it's trying to
look for the snippet ID rather than the book on approval action. I don't
understand why?
<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
Routes
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

No comments:

Post a Comment