0

所以我在我的 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
4

1 回答 1

1

从您提供的路由错误来看,它似乎正在尝试发布到“显示”操作。

尝试使用以下内容:

<%= form_for @user, :url => { :action => "create" } do |user| %>
于 2013-01-07T20:08:36.617 回答