Ruby on rails 我们都有。谢谢,你好。谢谢你的回答。我想我会为后代回答,因为另一个答案解决了我的问题。我以前尝试过这个解决方案,但Desive本身应该处理:email、:password和:password_确认参数。您只需要添加自定义的。谢谢 Started P

Ruby on rails 我们都有。谢谢,你好。谢谢你的回答。我想我会为后代回答,因为另一个答案解决了我的问题。我以前尝试过这个解决方案,但Desive本身应该处理:email、:password和:password_确认参数。您只需要添加自定义的。谢谢 Started P,ruby-on-rails,devise,Ruby On Rails,Devise,我们都有。谢谢,你好。谢谢你的回答。我想我会为后代回答,因为另一个答案解决了我的问题。我以前尝试过这个解决方案,但Desive本身应该处理:email、:password和:password_确认参数。您只需要添加自定义的。谢谢 Started POST "/users" for 127.0.0.1 at 2014-10-29 10:37:33 +0000 Processing by UsersController#create as HTML Parameters: {"utf8"


我们都有。谢谢,你好。谢谢你的回答。我想我会为后代回答,因为另一个答案解决了我的问题。我以前尝试过这个解决方案,但Desive本身应该处理:email、:password和:password_确认参数。您只需要添加自定义的。谢谢
    Started POST "/users" for 127.0.0.1 at 2014-10-29 10:37:33 +0000
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"8U+XxLIrE7MjOphsuorOwarggyZsj
3qTNQeap273QTo=", "user"=>{"name"=>"John Smith", "role"=>"MG", "client"=>""
, "email"=>"john@johnsmith.com", "password"=>"[FILTERED]", "passw
ord_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.0ms)  begin transaction
   (1.0ms)  rollback transaction
  Rendered users/new.html.erb within layouts/application (6.0ms)
  User Load (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORD
ER BY "users"."id" ASC LIMIT 1
  Rendered layouts/_header.html.erb (4.1ms)
#<ActiveModel::Errors:0x422fc88 @base=#<User id: nil, email: "", encrypted_passw
ord: "", reset_password_token: nil, reset_password_sent_at: nil, remember_create
d_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, curr
ent_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, nam
e: nil, client: nil, role: nil>, @messages={:email=>["can't be blank"], :passwor
d=>["can't be blank"], :name=>[], :role=>[], :client=>[], :password_confirmation
=>[]}>
Completed 422 Unprocessable Entity in 238ms (Views: 231.3ms | ActiveRecord: 1.0m
s)
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
 protect_from_forgery with: :exception

 before_filter :configure_permitted_parameters, if: :devise_controller?

 def after_sign_in_path_for(user)
    projects_path
 end

 protected

 def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) << :name
    devise_parameter_sanitizer.for(:account_update) << :client
    devise_parameter_sanitizer.for(:sign_up) << [:name, :role, :client]
 end
end
<%= form_for(@user) do |f| %>
   <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> stopped this user from being created:</h2>

      <ul>
      <% @user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div><%= f.label :name %><br />
    <%= f.text_field :name %></div>

  <div><%= f.label :role %><br />
  <%= select :user, :role, options_for_select(@role) %></div>

<div id="client_input"><%= f.label :client %><br />
    <%= f.text_field :client %></div>


  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <div><%= f.label :password %> <% if @validatable %><i>(<%= @minimum_password_length %> characters minimum)</i><% end %><br />
    <%= f.password_field :password, autocomplete: "off" %></div>

  <div><%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %></div>


  <div><%= f.submit "Sign up" %></div>
<% end %>
class UsersController < ApplicationController
    before_filter :get_user, :only => [:index,:new,:edit]
  before_filter :client_restriction, only: [:index, :show, :new]


  def index
    @users = User.all
    respond_to do |format|
      format.json { render :json => @users }
      format.xml  { render :xml => @users }
      format.html
    end
  end

  def new
    @user = User.new
    @role = ["MG", "Client"]
  end

  def show
  @user = User.all
    @projects = Project.all      
  end


  def edit
    @user = User.find(params[:id])
    @role = ["MG", "Client"]
    end

   def update
    @user = User.find(params[:id])
    @role = ["MG", "Client"]
    @user.client == :client
    @user.role == :role

    if params[:user][:password].blank?
      [:password, :password_confirmation, :current_password].collect{|p| params[:user].delete(p)}
    else
      @user.errors[:base] << "The password you entered is incorrect." unless @user.valid_password?(params[:user][:current_password])
    end 
    respond_to do |format|
      if @user.update(user_params)
        sign_in(@user, :bypass => true) # To counter weird Devise error which logs users out after password change
        format.html { redirect_to projects_path, notice: 'Your account details were updated successfully' }
      else
        format.html { render :edit }
      end
    end
  end

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

    respond_to do |format|
      format.html { redirect_to root_path, notice: "Your account has been deleted. Sorry to see you go!" }      
    end
    end

  def create
    @role = ["MG", "Client"]
    @user = User.new
    if @user.save
      respond_to do |format|
        format.html { redirect_to :back, notice: "New user successfully created" }
      end
    else
      respond_to do |format|
        format.json { render :text => "Could not create user", :status => :unprocessable_entity } # placeholder
        format.xml  { head :ok }
        format.html { render :action => :new, :status => :unprocessable_entity }
      end
    end
    Rails.logger.info(@user.errors.inspect)
    end 


  def add_user
  end

    private 

  def get_user
    @current_user = current_user
  end
  def client_restriction
    redirect_to root_path, notice: "You are not authorised to create users" if current_user.role != "MG"
  end  
  def user_params
      params.require(:user).permit(:role, :name, :client, :email, :password, :password_confirmation, :authenticity_token, project: [:name, :client, :phase], section: [:title, :position, :project_id], deliverable: [:file, :preview, :title, :project_id, :section_id], link: [:hyperlink, :title, :project_id, :section_id], embed: [:section_id, :embed_link, :title, :project_id])
    end

end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, #:registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :members
  has_and_belongs_to_many :projects
  has_many :sections    
  has_many :deliverables
  has_many :embeds
  has_many :links   

  accepts_nested_attributes_for :projects

end
def create
  # ...
  @user = User.new user_params
  # ...
end
def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) << :name
    devise_parameter_sanitizer.for(:account_update) << :client
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password,:password_confirmation, :name, :role, :client) }
end