Rails Devise - 从另一个控制器更新列 [英] Rails Devise - Update column from another controller

查看:174
本文介绍了Rails Devise - 从另一个控制器更新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 3.0

按照以下说明操作:
https://github.com/plataformatec/devise/wiki/How-To%3a-Require-admin-to-activate-account-before-sign_in
我已经生成了一个迁移:已批准(布尔)为我的设计user.rb。现在我想用不同控制器的复选框来编辑它:unapproved_users_controller.rb。

当我在编辑中加载表单时,我得到这个错误:undefined method`user_path'。

Rails 3.0
Following these instructions: https://github.com/plataformatec/devise/wiki/How-To%3a-Require-admin-to-activate-account-before-sign_in I've generated a migration :approved (boolean) for my devise user.rb. Now I want to edit it with a checkbox from a different controller: unapproved_users_controller.rb.
When I load the form in the edit I get this error: undefined method `user_path'.

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

routes.rb, the resources for my new controller

resources :unapproved_users 

app / models / user.rb ,请注意:批准的是attr_accessible。

app/models/user.rb, notice that :approved is 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 / p>

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'


推荐答案

你需要更改form_for。

You need to change the form_for.

应该是

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

这篇关于Rails Devise - 从另一个控制器更新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆