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は、ApplicationControllerから継承するadminセクションのホームページにすぎません。

これが私のモデルです:

  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 に答える