【问题标题】:Rails Devise - Update column from another controllerRails Devise - 从另一个控制器更新列
【发布时间】:2012-09-07 13:11:53
【问题描述】:

Rails 3.0
按照这些说明: https://github.com/plataformatec/devise/wiki/How-To%3a-Require-admin-to-activate-account-before-sign_in 我已经为我的设计 user.rb 生成了一个迁移 :approved (boolean)。现在我想使用来自不同控制器的复选框对其进行编辑:unapproved_users_controller.rb。
当我在编辑中加载表单时,出现此错误:未定义的方法 `user_path'。

routes.rb,我的新控制器的资源

resources :unapproved_users 

app/models/user.rb,注意 :approved 是 attr_accessible。

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :approved

  def active_for_authentication? 
    super && approved? 
  end 

  def inactive_message 
    if !approved? 
      :not_approved 
    else 
      super # Use whatever other message 
    end 
  end

  def self.send_reset_password_instructions(attributes={})
    recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
    if !recoverable.approved?
      recoverable.errors[:base] << I18n.t("devise.failure.not_approved")
    elsif recoverable.persisted?
      recoverable.send_reset_password_instructions
    end
    recoverable
  end
end

app/controllers/unapproved_controllers.rb

class UnapprovedUsersController < ApplicationController

  def index
    if params[:approved] == "false"
      @users = User.find_all_by_approved(false)
    else
      @users = User.all
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    @user.update_attributes(params[:user])
  end  

end

app/views/unapproved_users/index.html.haml

%h1 Users

= link_to "All Users", :action => "index"
|
= link_to "Users awaiting approval", :action => "index", :approved => "false"

%table
    - @users.each do |user|
        %tr
            %td= user.email
            %td= user.approved
            %td= link_to "Edit", edit_unapproved_user_path(user)

app/views/unapproved_users/edit.html.haml

= render 'form'

app/views/unapproved_users/_form.html.haml

= form_for (@user) do |f|

  -if @user.errors.any?
    #error_explanation
      %h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:"
      %ul
        - @user.errors.full_messages.each do |msg|
          %li= msg

  .field
    = f.label :approved, 'Approved?'
    = f.check_box :approved 
  .actions
    = f.submit 'Save'

【问题讨论】:

    标签: ruby-on-rails-3 devise


    【解决方案1】:

    您需要更改 form_for。

    应该是

    = form_for(@user, :url => unapproved_user_path(@user)) do |f|
    

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多