ruby on rails - Manage users through a CRUD interface with devise errors -
i have establish crud interface devise below code, problem running when try create user following errors. can't edit either. please help.
"2 errors prohibited user being saved: email can't blank password can't blank" of course have been entering of information inclduding email , password. follow documentation devise , have read of site devise topics no such problem reported. controller: class userscontroller < applicationcontroller before_filter :authenticate_user! before_action :authorized_user # /users # /users.json def index @users = user.all
# respond_to |format| # format.html index.html.erb # format.json { render :json => @users } end # /users/1 # /users/1.json def show @user = user.find(params[:id]) # respond_to |format| # format.html show.html.erb # format.json { render :json => @user } end # /users/new # /users/new.json def new @user = user.new # respond_to |format| # format.html new.html.erb # format.json { render :json => @user } end # /users/1/edit def edit @user = user.find(params[:id]) end # post /users # post /users.json def create @user = user.new(params[:user_params]) respond_to |format| if @user.save format.html { redirect_to @user, :notice => 'user 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 if params[:user][:password].blank? params[:user].delete(:password) params[:user].delete(:password_confirmation) end @user = user.find(params[:id]) respond_to |format| if @user.update_attributes(params[:user_params]) format.html { redirect_to @user, :notice => 'user updated.' } format.json { head :ok } else format.html { render :action => "edit" } format.json { render :json => @user.errors, :status => :unprocessable_entity } end end end # delete /users/1 # delete /users/1.json def destroy @user = user.find(params[:id]) @user.destroy respond_to |format| format.html { redirect_to users_url } format.json { head :ok } end end private def user_params params.require(:user).permit(:username, :name, :lastname, :email, :password, :password_confirmation, :role_id) end def authorized_user if current_user.role.name == "admin" else redirect_to :root, notice: "not authorized" if @current_user.role.name == 'user' or nil? end end
end
here form:
<%= form_for(@user) |f| %> <% if @user.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@user.errors.count, "error") %> prohibited user being saved:</h2> <ul> <% @user.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :username %><br /> <%= f.text_field :username, autofocus: true %> </div> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name, autofocus: true %> </div> <div class="field"> <%= f.label :lastname %><br /> <%= f.text_field :lastname, autofocus: true %> </div> <div class="field"> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true %> </div> <div class="field"> <%= f.label :password %> <% if @minimum_password_length %> <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="field"> <%= f.label :role_id %><br /> <%= f.collection_select :role_id, role.all, :id, :name_role_select %> </div> <div class="actions"> <%= f.submit "sign up", class: 'button' %> </div>
you aren't using user_params
.
instead of in create action:
user.new(params[:user_params])
you need this, calls user_params
method defined in controller:
user.new(user_params)
your original way looking params passed in request called 'user_params'(which doesn't exist).
similarly, update
action need:
if @user.update_attributes(user_params) #etc...
Comments
Post a Comment