English 中文(简体)
如何使只有经认证的用户能够创造新的用户
原标题:how to enable only authenticated users to create new users

I guess the title is self explanatory...how do I make only authenticated users to create a user with devise?
when I try to add a new one (when authenticated), devise says that I m alreade authenticated
any workaround for this?
thanks a lot

编辑:根据要求,我的代码

<<>strong>user form

<% if @user == current_user %>
<i class="icon-info-sign"> </i><i>Após alterar seus dados, você terá de fazer login novamente</i>
<% end %>

<%= form_for(@user, :html => { :class => "well form-horizontal"}) do |f| %>
  <% if @user.errors.any? %>
    <div class="alert alert-error">
      <h2><%= pluralize(@user.errors.count, "erro") %> 
        <% if @user.errors.count == 1 %> impede 
          <% else %>
            impedem
        <% end %>
      a continuação:</h2>

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


<label>Nome</label><%= f.text_field :nome %>
<label>E-mail</label><%= f.email_field :email %>

<% if @user == current_user or @user.created_at == nil%>
  <label>Senha</label><%= f.password_field :password %>
<% end %>

<div>
<br />
<button class="btn" type="submit">
    Salvar alterações
</button>

</div>

<% end %>

<>>部分用户控制器

before_filter :authenticate_user!
  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end

  # GET /users/1
  # GET /users/1.json
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user }
    end
  end

  # GET /users/new
  # GET /users/new.json
  def new
    @user = User.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end

  # GET /users/1/edit
  def edit
    @user = User.find(params[:id])
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice:  User was successfully created.  }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /users/1
  # PUT /users/1.json
  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice:  User was successfully updated.  }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

<>可见>

class ApplicationController < ActionController::Base
  protect_from_forgery

  def after_sign_in_path_for(resource)
        return request.env[ omniauth.origin ] || stored_location_for(resource) || painel_path
    end
end

<>部分>

申请:Application.routes.draw do

  resources :acampantes

  devise_for :users, :skip => [:registrations]

  resources :profiles

  resources :eventos do
    resources :acampantes
  end  

  # aqui tem um has_many, mas não precisa de nested routes
  resources :noticias
  resources :users#, :as => "usuarios"
最佳回答

理想的办法是取消<代码>登记_ Controller.rb(见here)和绕过之前的过滤器核对new/code>和create。 我试图亲自这样做,但不幸的是,我无法这样做。

Another way to do it, which worked for me, would be to create a separate User controller aside from the Registrations controller provided by Devise:

class UsersController < ApplicationController
  before_filter :authenticate_user!

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:success] = "User saved"
      redirect_to @user
    else
      render  new 
    end
  end
end

# routes.rb
ApplicationName::Application.routes.draw do
  devise_for :users
  resources :users, :only => [:new, :create]
end

然后,采取新行动的形式:

# app/views/users/new.html.erb
<%= form_for(@user) do |f| %>

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

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

  <div><%= f.submit "Sign up" %></div>
<% end %>

这种做法的问题是,Devise为签署提供的路线仍然存在。 解决办法是防止开发涉及登记的道路:

# routes.rb
devise_for :users, :skip => [:registrations]

This means that you are going to ignore the Registrations module completely and make your own. I got this idea from the Bare-bone, stripped-down Devise tutorial. Take note that you should make the controller actions and views to replace the ones provided by the registrations module of Devise. This includes new, edit, create, update, and destroy.

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...