【问题标题】:Rails 4 + Devise Separate User CRUDRails 4 + 设计单独的用户 CRUD
【发布时间】:2015-02-22 16:59:24
【问题描述】:

我正在使用 Rails 4 构建一个简单的管理界面,用于添加和删除用户或 CRUD。

我目前正在安装 Devise 并构建了基本视图和操作。

但是,在我自己填写表格后,我得到了以下信息:

2 个错误导致该用户无法保存:

  • 电子邮件不能为空
  • 密码不能为空
  • 这是我目前拥有的:(由于我不想公开注册,所以删除了可注册)

    模型 > user.rb

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

    视图 > 用户 > new.html.erb

    <%= form_for @user do |f| %>
      <% if @user.errors.any? %>
        <div class="error_explanation">
          <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
    
          <ul>
          <% @user.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>
    
      <div class="field">
        <%= f.label :email %><br />
        <%= f.email_field :email, autofocus: true %>
      </div>
    
      <div class="field">
        <%= f.label :password %>
        <% if @validatable %>
        <em>(<%= @minimum_password_length %> characters minimum)</em>
        <% end %><br />
        <%= f.password_field :password, autocomplete: "off" %>
      </div>
    
      <div class="field">
        <%= f.label :password_confirmation %><br />
        <%= f.password_field :password_confirmation, autocomplete: "off" %>
      </div>
    
      <div class="actions">
        <%= f.submit "Create User" %>
      </div>
    
    <% end %>
    

    routes.rb(目前我通过“/admin/users/new”创建新用户)

    Rails.application.routes.draw do
    
      devise_for :users
      scope '/admin' do
        resources :users
      end
    end
    

    控制器 > users_controller.rb

    class UsersController < ApplicationController
      def index
        @users = User.all
      end
    
      def show
        @user = User.find(params[:id])
      end
    
      def new
        @user = User.new
      end
    
      def edit
        @user = User.find(params[:id])
      end
    
      def create
        @user = User.new(params[user_params])
    
        if @user.save
          redirect_to @user, :flash => { :success => 'User was successfully created.' }
        else
          render :action => 'new'
        end
      end
    
      def update
        @user = User.find(params[:id])
    
        if @user.update_attributes(params[user_params])
          sign_in(@user, :bypass => true) if @user == current_user
          redirect_to @user, :flash => { :success => 'User was successfully updated.' }
        else
          render :action => 'edit'
        end
      end
    
      def destroy
        @user = User.find(params[:id])
        @user.destroy
        redirect_to users_path, :flash => { :success => 'User was successfully deleted.' }
      end
    
      private
        def user_params
          params.require(:user).permit(:email, :password, :password_confirmation)
        end
    end
    

    有人对导致错误的原因有任何想法吗? 谢谢:D

    请注意:以前,在我在 user.rb 的 devise_modules 中禁用 :registerable 之前,我实际上可以使用默认的设计视图“users/sign_up”执行注册。但是我想要的仍然是获得一个管理员 CRUD 系统,所以我还是禁用了它。

    【问题讨论】:

    • 出于好奇,如果您需要管理界面来执行 CRUD 操作,为什么不使用rails admin
    • 是的 rails_admin 应该是管理员的选择。顺便说一句,您是否对用户模型进行了验证?
    • 嗨 @mmichael 一定错过了,它看起来很棒,我注意到的一件事是最后一次构建失败(在 github 页面中)?将进一步研究它。

    标签: ruby-on-rails ruby ruby-on-rails-4 devise


    【解决方案1】:

    你的def create/update是不是错字:

    @user = User.new(params[user_params])
    # should be
    @user = User.new(user_params)
    
    if @user.update_attributes(params[user_params])
    # should be
    if @user.update_attributes(user_params)
    

    【讨论】:

    • 哇,真不敢相信我错过了。虽然没有通知我这个错误,但有点恼火。现在可以了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    相关资源
    最近更新 更多