Rails Dot in Url with two routes for one controller - ruby-on-rails

I'm making a site with one controller "Projects" and i want to show all projects with to routes :
/admin/projects/:id = /admin/projects/1 (works)
/front/:id = /front.1 (doesn't work)
I have tried this get 'front/:id' => 'projects#show', :constraints => { :id => /[^/]+/ } in route.rb but it doesn't work.
My files :
routes.rb
Rails.application.routes.draw do
resources :users, path: '/admin/clients'
get 'admin' => 'admin#dashbord'
get 'admin/profile'
get 'admin/settings'
get 'admin/_admin_header'
get 'front' => 'front#index'
get 'front/profile' => 'front#profile'
get 'front/:id' => 'projects#show'
scope '/admin' do
resources :projects do
resources :pictures
end
end
end
projects_controller.rb
layout 'adminApplication'
before_action :set_project, only: [:show, :edit, :update, :destroy]
def index
#projects = Project.all
end
def show
end
def new
#project = Project.new
end
def edit
end
def create
#project = Project.new(project_params)
respond_to do |format|
if #project.save
format.html { redirect_to #project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: #project }
else
format.html { render :new }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #project.update(project_params)
format.html { redirect_to #project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: #project }
else
format.html { render :edit }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
def destroy
#project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_project
#project = Project.find(params[:id])
end
def project_params
params.require(:project).permit(:name, :date, :location, :integer)
end
end
front_controller.rb
def index
#projects = Project.all
render 'projects/index'
end
def show
end
def profile
end
end
in projects/index.html.erb
- link_to 'Show', project
- link_to 'Show', front_path(project)
I already checked all similar questions.
Thanks for your help !
Kazei Design
Update
rake routes | grep front:
front GET /front(.:format) front#index
front_profile GET /front/profile(.:format) front#profile
GET /front/:id(.:format) projects#show

You're using a named route helper, but you didn't specify it:
- link_to 'Show', front_path(project)
And you can see in your routes that front_path for projects#show doesn't exist:
rake routes | grep front
front GET /front(.:format) front#index
front_profile GET /front/profile(.:format) front#profile
GET /front/:id(.:format) projects#show
So, in your routes.rb add the helper:
get 'front/:id' => 'projects#show', as: :custom_front
And now run rake routes to see the new helper (it should be custom_front_path) and use it:
- link_to 'Show', custom_front_path(project)
See more info in the documentation - 4.3 Overriding the Named Helpers

You might want to change your route to
get 'front.:id' => 'projects#show'

Related

Rails: deep nested resources gives error: undefined method 'comments'

I'm getting this error undefined method 'comments' in my rails application. I know I shouldn't do a >level 1 nested resources, but I don't know how to apply the correct way in this case.
Currently this is my routes:
resources :performance_indicators do
resources :improvement_actions do
member do
put "like" => "improvement_actions#upvote"
put "unlike" => "improvement_actions#downvote"
end
resources :comments
end
end
As I said I'm getting this error:
NoMethodError in PerformanceIndicators#show
Showing .../app/views/comments/_form.html.erb where line #1 raised:
undefined method `comments' for nil:NilClass
I don't know if my problem is in the controller. Anyone can help? :)
EDIT:
My comment/_form:
<%= form_for([#performance_indicator, #improvement_action, #improvement_action.comments.build]) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is my CommentsController:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :set_improvement_action
# GET /comments
# GET /comments.json
def index
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
#comment = #improvement_action.comments.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
#comment = #improvement_action.comments.new(comment_params)
if #comment.save
format.html { redirect_to [#improvement_action.performance_indicator, #improvement_action], notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: [#improvement_action, #comment] }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if #comment.update(comment_params)
format.html { redirect_to #comment, notice: 'Comment was successfully updated.' }
format.json { render :show, status: :ok, location: #comment }
else
format.html { render :edit }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:body)
end
def set_improvement_action
#improvement_action = ImprovementAction.includes(:comments).find(params[:improvement_action_id])
end
end
Here Is my PerformanceIndicatorController:
class PerformanceIndicatorsController < ApplicationController
before_action :set_performance_indicator, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /performance_indicators
# GET /performance_indicators.json
def index
#performance_indicators = PerformanceIndicator.all
end
# GET /performance_indicators/1
# GET /performance_indicators/1.json
def show
##performance_indicators = PerformanceIndicator.all.order("created_at DESC")
end
# GET /performance_indicators/new
def new
#performance_indicator = PerformanceIndicator.new
#comments = Comment.new
end
# GET /performance_indicators/1/edit
def edit
end
# POST /performance_indicators
# POST /performance_indicators.json
def create
#performance_indicator = PerformanceIndicator.new(performance_indicator_params)
respond_to do |format|
if #performance_indicator.save
format.html { redirect_to #performance_indicator, notice: 'Performance indicator was successfully created.' }
format.json { render :show, status: :created, location: #performance_indicator }
else
format.html { render :new }
format.json { render json: #performance_indicator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /performance_indicators/1
# PATCH/PUT /performance_indicators/1.json
def update
respond_to do |format|
if #performance_indicator.update(performance_indicator_params)
format.html { redirect_to #performance_indicator, notice: 'Performance indicator was successfully updated.' }
format.json { render :show, status: :ok, location: #performance_indicator }
else
format.html { render :edit }
format.json { render json: #performance_indicator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /performance_indicators/1
# DELETE /performance_indicators/1.json
def destroy
#performance_indicator.destroy
respond_to do |format|
format.html { redirect_to performance_indicators_url, notice: 'Performance indicator was successfully destroyed.' }
format.json { head :no_content }
end
end
def set_comment
#improvement_action = ImprovementAction.find(params[:improvement_action_id])
#comment = Comment.find(params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_performance_indicator
#performance_indicator = PerformanceIndicator.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def performance_indicator_params
params.require(:performance_indicator).permit(:name, :numberTimesIdentifiedProblems, :numberTimesAnalysed)
end
end
Lets start by fixing the deep nesting.
resources :performance_indicators, shallow: true do
resources :improvement_actions
end
resources :improvement_actions, only: [] do
member do
put "like" => "improvement_actions#upvote"
put "unlike" => "improvement_actions#downvote"
end
resources :comments
end
only: [] is a clever trick that nests routes under a resource but suppresses the generation of routes. We want that since the first block actually declares all the routes we need.
Prefix Verb URI Pattern Controller#Action
performance_indicator_improvement_actions GET /performance_indicators/:performance_indicator_id/improvement_actions(.:format) improvement_actions#index
POST /performance_indicators/:performance_indicator_id/improvement_actions(.:format) improvement_actions#create
new_performance_indicator_improvement_action GET /performance_indicators/:performance_indicator_id/improvement_actions/new(.:format) improvement_actions#new
edit_performance_indicator_improvement_action GET /performance_indicators/:performance_indicator_id/improvement_actions/:id/edit(.:format) improvement_actions#edit
performance_indicator_improvement_action GET /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#show
PATCH /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#update
PUT /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#update
DELETE /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#destroy
performance_indicators GET /performance_indicators(.:format) performance_indicators#index
POST /performance_indicators(.:format) performance_indicators#create
new_performance_indicator GET /performance_indicators/new(.:format) performance_indicators#new
edit_performance_indicator GET /performance_indicators/:id/edit(.:format) performance_indicators#edit
performance_indicator GET /performance_indicators/:id(.:format) performance_indicators#show
PATCH /performance_indicators/:id(.:format) performance_indicators#update
PUT /performance_indicators/:id(.:format) performance_indicators#update
DELETE /performance_indicators/:id(.:format) performance_indicators#destroy
like_improvement_action PUT /improvement_actions/:id/like(.:format) improvement_actions#upvote
unlike_improvement_action PUT /improvement_actions/:id/unlike(.:format) improvement_actions#downvote
improvement_action_comments GET /improvement_actions/:improvement_action_id/comments(.:format) comments#index
POST /improvement_actions/:improvement_action_id/comments(.:format) comments#create
new_improvement_action_comment GET /improvement_actions/:improvement_action_id/comments/new(.:format) comments#new
edit_improvement_action_comment GET /improvement_actions/:improvement_action_id/comments/:id/edit(.:format) comments#edit
improvement_action_comment GET /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#show
PATCH /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#update
PUT /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#update
DELETE /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#destroy
Un-nesting the resources means there will be a lot less hoops to jump through.
class CommentsController < ApplicationController
before_action :set_improvement_action
# GET /improvement_actions/:improvement_action_id/comments/new
def new
#comment = #improvement_action.comments.new
end
# POST /improvement_actions/:improvement_action_id/comments
def create
#comment = #improvement_action.comments.new(comment_params) do |c|
# #todo - you should associate comment with the user who created it at some point.
# c.author = current_user
end
# note that you where saving the record twice!
if #comment.save
format.html { redirect_to [#improvement_action.performance_indicator, #improvement_action], notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: [#improvement_action, #comment] }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
# ...
private
def comment_params
params.require(:comment).permit(:body)
end
def set_improvement_action
#improvement_action = ImprovementAction.includes(:comments)
.find(params[:improvement_action_id])
end
end
You should handle seeding with new records on the controller side if possible.
<%= form_for([#improvement_action, #comment]) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Nested Namespace Update Action No route matches [PATCH]

Been trying to progress through rails using namspacing, there's been similar questions I think my problem is everything is namespaced.
I tried adding a class to the models (did that before with devise user with nest object and worked) but it doesn't seem to work when dealing with namespaces.
I get a routing error.
No route matches [PATCH] "/backend/membercontacts/1/memberlistings"
routes.rb
# Application Client Backend
namespace :backend do
# Member Routes
resources :membercontacts do
resources :memberaddresses
resources :memberlistings
end
end
backend/memberlisting.rb
class Backend::Memberlisting < ActiveRecord::Base
# Model Relationships
belongs_to :membercontact
end
backend/membercontact.rb
class Backend::Membercontact < ActiveRecord::Base
# Model Relationship
has_many :memberlistings, dependent: :destroy
end
backend/memberlistings_controller.rb
class Backend::MemberlistingsController < ApplicationController
# Security & Action Filters
layout '/backend/application.html.erb'
before_action :set_memberlisting, only: [:show, :edit, :update, :destroy]
# Member Listing Index
def index
membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlistings = membercontact.memberlistings.order('mlcontactname ASC')
end
# Detailed Member Listing Profile
def show
membercontact = Backend::Membercontact.find(params[:membercontact_id])
#membercontact = membercontact.memberlistings.find(params[:id])
end
# New Member Listing
def new
membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlisting = membercontact.memberlistings.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #membercontact }
end
end
# Edit Member Listing
def edit
membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlisting = membercontact.memberlistings.find(params[:id])
end
# Create Member Listing Action
def create
#membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlisting = #membercontact.memberlistings.create(memberlisting_params)
respond_to do |format|
if #memberlisting.save
format.html { redirect_to backend_membercontact_memberlistings_path, notice: 'Address for Membercontact was Successfully Created.' }
format.json { render action: 'show', status: :created, location: #memberlisting }
else
format.html { render action: 'new' }
format.json { render json: #memberlisting.errors, status: :unprocessable_entity }
end
end
end
# Update Member Listing Action
def update
respond_to do |format|
if #memberlisting.update(memberlisting_params)
format.html { redirect_to backend_membercontact_memberlistings_path, notice: 'Address for Membercontact was Successfully Updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #memberlisting.errors, status: :unprocessable_entity }
end
end
end
# Delete Member Listing Action
def destroy
#membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlisting = #membercontact.memberlistings.find(params[:id])
#memberlisting.destroy
respond_to do |format|
format.html { redirect_to backend_membercontact_memberlistings_path, notice: 'Address for Membercontact was Successfully Deleted.' }
format.json { head :no_content }
end
end
private
def set_memberlisting
#memberlisting = Backend::Memberlisting.find(params[:id])
end
def memberlisting_params
params.require(:backend_memberlisting).permit(:mlcontactname, :mlcompanyname, :mladdressline1, :mladdressline2, :mlcity, :mlprovince, :mlpostalcode, :mlphone, :mlwebsite, :mlemail, :membercontact_id, :account_id)
end
end
Routes Rake
backend_membercontact_memberlistings_path GET /backend/membercontacts/:membercontact_id/memberlistings(.:format) backend/memberlistings#index
POST /backend/membercontacts/:membercontact_id/memberlistings(.:format) backend/memberlistings#create
new_backend_membercontact_memberlisting_path GET /backend/membercontacts/:membercontact_id/memberlistings/new(.:format) backend/memberlistings#new
edit_backend_membercontact_memberlisting_path GET /backend/membercontacts/:membercontact_id/memberlistings/:id/edit(.:format) backend/memberlistings#edit
backend_membercontact_memberlisting_path GET /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format) backend/memberlistings#show
PATCH /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format) backend/memberlistings#update
PUT /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format) backend/memberlistings#update
DELETE /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format) backend/memberlistings#destroy
So far I've tried to work with classes (still learning) in the models file and have tried some variations like below. Also the reason why I define the url path, it defaulted to backend_membercontact_backend_memberlisting
, class_name: 'Backend::Memberlisting'
<%= form_for([#memberlisting.membercontact, #memberlisting], url: backend_membercontact_memberlistings_path, method: :post) do |f| %>
No route matches [PATCH] "/backend/membercontacts/1/memberlistings"
From your rake routes output, the path is backend_membercontact_memberlisting_path( no s memberlisting) not backend_membercontact_memberlistings_path
Also, you should change method: post to method: :patch
<%= form_for([#memberlisting.membercontact, #memberlisting], url: backend_membercontact_memberlisting_path, method: :patch) do |f| %>
Try to use
<%= form_for([#memberlisting.membercontact, #memberlisting], url: backend_membercontact_memberlisting_path, method: :post) do |f| %>
Your are using backend_membercontact_memberlistings_path according to your routes is should be backend_membercontact_memberlisting_path
Hope this works.

No route matches {:action=>"show", :controller=>"controller_name"}

I'm always getting this error and I don't understand where I am wrong. I think I have everything I need action in controller, resources in route file and view for controller action. I put the route current_events/new in the browser when I get this error. I also try with just resources :current_events
output of rake routes:
current_events GET /current_events(.:format) current_events#index
new_current_event GET /current_events/new(.:format) current_events#new
current_event GET /current_events/:id(.:format) current_events#show
config/routes.rb:
appname::Application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks"}
resources :current_events, only: [:show, :new, :index]
end
CurrentEventsController:
class CurrentEventsController < ApplicationController
def index
#current_event = CurrentEvent.all
respond_to do |format|
format.html
format.json { render json: #current_event }
end
end
def create
#current_event = CurrentEvent.new(params[:current_event])
respond_to do |format|
if #current_event.save
format.html { redirect_to #current_event, notice: 'current event was created.' }
format.json { render json: #current_event, status: :created, location: #current_event }
else
format.html { render action: "new" }
format.json {render json: #current_event.errors, status: :unprocessable_entity}
end
end
end
def new
#current_event = CurrentEvent.new
respond_to do |format|
format.html
format.json { render json: #current_event }
end
end
def edit
#current_event = CurrentEvent.find(params[:id])
end
def destroy
#current_event = CurrentEvent.find(params[:id])
#current_event.destroy
respond_to do |format|
format.html { redirect_to current_event_url}
format.json { head :no_content }
end
end
def show
#current_event = CurrentEvent.find(params[:id])
respond_to do |format|
format.html
format.json{ render json: #current_event}
end
end
end
I forgot to say, I am trying to go to new page so in browser I say current_events/new
It will still throw an error if there's a link that doesn't work on the page.
In your view, the link should look something like this:
<%= link_to "name of current event", current_event_path(#current_event) %>
update
based on your rake routes
current_event GET /current_events/:id(.:format) #note ":id"
when you're trying to see a specific current event, you need to pass it an :id which makes sense - if you're trying to call a specific person you need to use their telephone number. so your code should look like this:
<%= link_to 'Back', current_event_path(#event) %>
But keep in the mind that #event won't work unless you define it correctly in the controller action for this view.

Ruby on Rails - Passing Parameters between methods

Routes File:
resources :tournaments do
resources :game, :only => [:new, :index, :create, :update, :destroy]
end
Rake Routes shows:
new_tournament_game GET /tournaments/:tournament_id/game/new(.:format) game#new
I call:
<td><%= link_to 'Add Game', new_tournament_game_path(tournament) %></td>
Game Model:
Takes me to the view game view with the URL:
http:// local host:3000/tournaments/2/game/new
And view:
<h1>New Game in <%= #tournament.name %> Tournament</h1>
<fieldset>
<%= form_for [:tournament, #game], :url => tournament_game_index_path do |f| %>
<table>
<td>
.... More fields .....
</td>
<div class="form-actions">
<%= f.submit "Create %>
</div>
<% end %>
When create is clicked it yields the error:
undefined method `game_url' for #<GamesController:0xb6131e40>
Questions:
Should I be using nested routes or hidden fields?
Do I need a separate tournament_game controller / view to handle the tournament game calls?
How do I get it to look for the correct create route when clicking the Create button?
When I want to have a relationship between two tables, do I only need the nested resource and the has_many / belongs_to calls or do I still need a foreign key column such as Tournament?
Sorry for all of the questions in one thread. Any help you can offer would be greatly appreciated!
Thank you.
Edit:
The error references line 39 which would be the line for the create controller.
class GamesController < ApplicationController
# GET /game
# GET /game.json
def index
#game = Game.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #game }
end
end
# GET /game/1
# GET /game/1.json
def show
#game = Game.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #game }
end
end
# GET /game/new
# GET /game/new.json
def new
#game = Game.new
#tournament = Tournament.find(params[:tournament_id])
end
# GET /game/1/edit
def edit
#game = Game.find(params[:id])
end
# POST /game
# POST /game.json
def create
#tournament = Tournament.find(params[:tournament_id])
#game = #tournament.game.build(params[:game])
respond_to do |format|
if params[:commit] != 'Cancel'
if #game.save
format.html { redirect_to #game, notice: 'Game was successfully created.' }
format.json { render json: #game, status: :created, location: #game }
format.json { render json: #game }
else
format.html { render action: "new" }
format.json { render json: #game.errors, status: :unprocessable_entity }
end
else
format.html { redirect_to #game, alert: 'Game was not updated.' }
end
end
end
# PUT /game/1
# PUT /game/1.json
def update
#game = Game.find(params[:id])
respond_to do |format|
if params[:commit] != 'Cancel'
if #game.update_attributes(params[:game])
format.html { redirect_to #game, notice: 'Game was successfully updated.' }
format.json { render json: #game }
else
format.html { render action: "edit" }
format.json { render json: #game.errors, status: :unprocessable_entity }
end
else
format.html { redirect_to #game, alert: 'Game was not updated.' }
end
end
end
# DELETE /game/1
# DELETE /game/1.json
def destroy
#game = Game.find(params[:id])
#game.destroy
respond_to do |format|
format.html { redirect_to game_url }
format.json { head :no_content }
end
end
end
Try this:
<%= form_for #game, :url => tournament_games_path(#tournament) do |f| %>
This will call the create method of games controller
Game Controller:
def create
#tournament = Tournament.find(params[:tournament_id])
#game = #tournament.games.build(params[:game])
if #game.save
flash[:success] = "Game created successfully"
redirect_to tournaments_path
else
render new_tournament_game_path
end
end
Routes:
resources :tournaments do
resources :games, :only => [:new, :index, :create, :update, :destroy]
end

Generating scaffolding for existing model

Newbie to RoR here. I have built models with no namespace. One of them is called 'Brand'. I then proceeded to use rails g "admin/brands" to put maintenance functionality under an admin namespace, using rails generate scaffolding_controller "admin/brand" - which produced the views and the controller. The unit tests fail when I rake test:
NoMethodError: undefined method `admin_brands' for #<Admin::BrandsControllerTest:0x1034c0730>
test/functional/admin/brands_controller_test.rb:5:in `_callback_before_193'
in routes.rb I have:
# Administration routes
namespace :admin do
resources :brands
end
The generated controller code is as follows:
class Admin::BrandsController < ApplicationController
# GET /admin/brands
# GET /admin/brands.json
def index
#admin_brands = Brand.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #admin_brands }
end
end
# GET /admin/brands/1
# GET /admin/brands/1.json
def show
#admin_brand = Brand.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #admin_brand }
end
end
# GET /admin/brands/new
# GET /admin/brands/new.json
def new
#admin_brand = Brand.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #admin_brand }
end
end
# GET /admin/brands/1/edit
def edit
#admin_brand = Brand.find(params[:id])
end
# POST /admin/brands
# POST /admin/brands.json
def create
#admin_brand = Brand.new(params[:admin_brand])
respond_to do |format|
if #admin_brand.save
format.html { redirect_to #admin_brand, :notice => 'Brand was successfully created.' }
format.json { render :json => #admin_brand, :status => :created, :location => #admin_brand }
else
format.html { render :action => "new" }
format.json { render :json => #admin_brand.errors, :status => :unprocessable_entity }
end
end
end
# PUT /admin/brands/1
# PUT /admin/brands/1.json
def update
#admin_brand = Brand.find(params[:id])
respond_to do |format|
if #admin_brand.update_attributes(params[:admin_brand])
format.html { redirect_to #admin_brand, :notice => 'Brand was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #admin_brand.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /admin/brands/1
# DELETE /admin/brands/1.json
def destroy
#admin_brand = Brand.find(params[:id])
#admin_brand.destroy
respond_to do |format|
format.html { redirect_to admin_brands_url }
format.json { head :no_content }
end
end
end
Not sure how to debug this type of issue... I gather that paths are messed up somehow, but that is much as I can fathom at this point. Help appreciated.
In our company we dont use scaffold, especially when we need to generate admin namespace.
You can simply write admin namespace yourself.
config/routes.rb
namespace :admin do
root :to => "base#index"
resources :pages
# resources :states do
# member do
# get :make_default
# end
# end
end
app/controllers/admin/base_controller.rb
class Admin::BaseController < ApplicationController
before_filter :authenticate_user!, :admin_user?
layout "admin/admin"
def index
#page = Page.all
end
private
def admin_user?
redirect_to root_path, :alert => 'This page is allowed for admin' unless current_user.admin
end
end
app/views/admin/base/index.html.haml
= link_to "New Post", new_admin_post_path
%ul
- #post.each do |post|
%li= post.title

Resources