所以我有一个管理部分在我的rails应用程序,在admin的名称空间和我的路由似乎一半坏了。在我的admin部分,我设置了一个用户资源,这样我就可以管理我的用户。索引视图工作只是找到,编辑视图工作,但创建操作被破坏,新视图工作,但添加一个表单破坏它,因为我的视图。
比如说。以下是我的路线:
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只是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
my form_for in new看起来像
<%= form_for [:admin, @user] do |user| %>
我也试过像我的编辑表单:
<%= form_for @user do |user| %>
但是这给了我路由错误:
No route matches {:action=>"show", :controller=>"admin/users",....}
和尝试编辑(提交表单)给了我这个错误:
uninitialized constant UsersController
从您提供的路由错误判断,它看起来像是试图发送到"show"动作。
尝试使用以下命令:
<%= form_for @user, :url => { :action => "create" } do |user| %>