所以我有一個管理部分在我的Rails應用中,在管理員的名稱空間和我的路線似乎有一半被打破。在我的管理部分中,我設置了用戶資源,以便我可以管理我的用戶。索引視圖正常工作,編輯視圖起作用,但創建操作被破壞,新視圖起作用,但添加表單會因爲我的視圖而破壞它。命名空間路由不工作
舉例來說。這裏是我的路線:
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的僅僅是管理部分的主頁,從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
您重新啓動您的serrver?確實如此, – MrYoshiji
。我正在看服務器,因爲我重新加載了「新」視圖,並注意到,無論出於何種原因,它期望的用戶ID:'用戶負載(0.2ms)SELECT'用戶'。*從'用戶'WHERE'用戶'.''id '= 1 LIMIT 1' – rugbert
您可以發佈您的用戶和報告模型嗎?如果用戶表單希望爲用戶構建報表,則可能會出現問題。在這種情況下,您必須將'user.reports.build'添加到您的'new'動作才能讓您的表單正常工作 –