【问题标题】:Wrong URL with simple_form_for带有 simple_form_for 的错误 URL
【发布时间】:2014-08-27 19:29:58
【问题描述】:

我在生成用于添加和编辑现有用户的表单时遇到问题。它会生成一个错误的 URL 来提交。我有以下型号:

用户:

class User < ActiveRecord::Base
    has_many :organization_users, :class_name => "::OrganizationUser", :foreign_key => :user_id, :dependent => :destroy
    has_many :organizations, :through => :organization_users

老师:

class User::Teacher < User

组织:

class Organization < ActiveRecord::Base 
    has_many :organization_users
    has_many :users, :through => :organization_users

组织用户:

class OrganizationUser < ActiveRecord::Base
    belongs_to :user, :class_name => "::User"
    belongs_to :organization

在我的路线文件中,我创建了以下规则:

namespace :manager do
    resources :organizations, :only => [:edit, :update], :controller => "organizations/organizations" do
          resources :users, :only => [:index, :show, :new, :create, :edit, :update, :destroy] do
  end
end

结束

用户控制器如下所示:

class Manager::UsersController < Manager::ApplicationController
      def new
        @user = user_scope.new
        respond_to do |format|
           format.html
        end
      end
      def edit
          respond_to do |format|
              format.html
          end
      end

这是我创建的表格:

= simple_form_for [:manager, @organization, @user], :as => :user, :html => {:class => 'form-horizontal', :multipart => true} do |f|
    = f.input :type, :as => :hidden, :input_html => {:name => :type, :value => @type}
    %ul{:class=>%w(nav nav-tabs)}
    ......

使用此表单,我可以毫无问题地创建新用户。问题是更新现有的,得到这个错误:

undefined method `manager_organization_user_teacher_path'

我不知道为什么 simple_form 会生成带有用户(老师)类型的 url,这个路径不存在。如何避免这种行为?

谢谢!

编辑

这里我展示了我如何加载@user

用户控制器:

class Manager::UsersController < Manager::ApplicationController
    require_power_check

    power :crud => :users, :as => :user_scope

    before_filter :load_record, :only => [:show,:edit,:update,:destroy]

  def load_record
      @user = user_scope.find(params[:id])
  end

这是我的用户范围权力:

    power :users do
        types = []
        types << 'User::Teacher' if can_organization_teacher_index?
        types << 'User::Student' if can_organization_student_index?
        types << 'User::Parent' if can_organization_parent_index?
        User.active.where("type in      (?)",types).includes(:organization_users).where('organization_users.organization_id'=>@organization)
    end

我已经尝试过 MaximusDominus 解决方案并且它有效,但很高兴知道它为什么会发生。

【问题讨论】:

    标签: ruby-on-rails ruby simple-form


    【解决方案1】:

    在您的新操作中,@userUser

    在您的编辑操作中,@userUser::Teacher

    我们需要查看您是如何加载 @user 资源的,以了解您为什么获得的是 User::Teacher 而不是 User。

    最简单的解决方法是becomes 方法:

    = simple_form_for [:manager, @organization, @user.becomes(User)], :as => :user...
    

    http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-becomes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多