命名空间路由无效

时间:2013-01-07 18:05:18

标签: ruby-on-rails ruby-on-rails-3

所以我在我的rails应用程序中有一个管理部分,在admin的名称空间中,我的路线似乎已经破了一半。在我的管理部分,我设置了用户资源,以便管理我的用户。索引视图只是查找,编辑视图有效,但创建操作被破坏,新视图有效,但添加表单会因为我的视图而中断它。

所以举个例子。这是我的路线:

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

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

我的用户控制器有:

  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只是admin部分的主页,它继承自ApplicationController

以下是我的模特:

  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

我的form_for看起来像

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

我也尝试过这样的编辑表格:

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

但这给了我路由错误:

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

并尝试编辑(提交表单)给出了这个错误:

uninitialized constant UsersController

1 个答案:

答案 0 :(得分:1)

根据您提供的路由错误判断,它似乎正在尝试发布到&#34; show&#34;动作。

尝试使用以下内容:

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