rugbert
rugbert

Reputation: 12673

Namespaced routes not working

So I have an admin section in my rails app, in the name space of admin and my routes seem half broken. In my admin section, I have a user resources set up so I can manage my users. The index view works just find, the edit view works, but the create action is broken, the new view works, but adding a form breaks it because my view.

So for example. Here are my routes:

namespace :admin do
  root :to => "home#index"

    resources :users do
        resources :reports, :only => ['show', 'destroy']
    end
        resources :reports, :only => ['show', 'destroy']
end

my users controller has:

  class Admin::UsersController < Admin::HomeController
  def index
        @users = User.all
  end

  def new
        @user = User.new
  end

    def create
        @user = User.new(params[:user])

        if @user.save
            redirect_to(edit_admin_user_path(@user), :notice => 'report was successfully created.')
        else
            render :action => "new"
        end
    end

  def edit
        @user = User.find(params[:id])
  end

    def update
        @user = User.find(params[:id])

        if @user.update_attributes(params[:user])
            redirect_to(edit_admin_user_path(@user), :notice => 'report was successfully created.')
        else
            render :action => "edit"
        end
    end

    def show
        @user = User.find(params[:id])
    end

    def destroy
        @user = User.find(prams[:id])
        @user.destroy

        redirect_to admin_users_path()
    end

end

HomeController is just the homepage for the admin section, which inherits from the ApplicationController

Here are my models:

  belongs_to :user
    has_many :receipts

  attr_accessible :cash_advance, :company, :description, :end_date, :mileage, :report_name,
  :start_date, :receipts_attributes

    validates_presence_of :company, :description, :end_date, :report_name#, :start_date
    validates_uniqueness_of :report_name

    accepts_nested_attributes_for :receipts, :allow_destroy => :true

class Receipt < ActiveRecord::Base
  belongs_to :report
  attr_accessible :account_code, :amount, :company_card, :date, :description, :lobbying_expense, :vendor

    validates_presence_of :date, :vendor, :amount, :description, :account_code
end

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable, :validatable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  # attr_accessible :title, :body

    has_many :reports, :dependent => :destroy
end

my form_for in new looks like

<%= form_for [:admin, @user] do |user| %>

I also tried this like my edit form:

<%= form_for @user do |user| %>

But that gives me the routing error:

No route matches {:action=>"show", :controller=>"admin/users",....}

and trying to edit (submitting the form) gives me this error:

uninitialized constant UsersController

Upvotes: 0

Views: 407

Answers (1)

Kevin B
Kevin B

Reputation: 999

Judging by the routing error you have supplied it looks like it is trying to post to the "show" action.

Try using the following:

<%= form_for @user, :url => { :action => "create" } do |user| %>

Upvotes: 1

Related Questions