ruby on rails - saving data in database fails rails4 -
i novice in ror , trying save new user database follow: in users_controller:
class userscontroller < applicationcontroller def index @users= user.all end def new @user= user.new end def create @user =user.new(user_params) if @user.save redirect_to root_path flash[:notice]='you new user now!' else render 'new' #flash[:notice]='oops!' end end private def user_params params.require(:user).permit(:l_name, :f_name, :email, :pseudo, :password_digest, :password_confirmation) end end
my user model:
class user < activerecord::base has_secure_password end
and new.html.erb this:
<h1>inscription</h1> <%= form_for (@user) |f| %> <div class="field"> <%= f.label :first_name %><br> <%= f.text_field :f_name %> </div> <div class="field"> <%= f.label :last_name %><br> <%= f.text_field :l_name %> </div> <div class="field"> <%= f.label :pseudo %><br> <%= f.text_field :pseudo %> </div> <div class="field"> <%= f.label :email %><br> <%= f.text_field :email %> </div> <div class="field"> <%= f.label :password %><br> <%= f.password_field :password_digest %> </div> <div class="field"> <%= f.label :password_confirmation %><br> <%= f.password_field :password_confirmation %> </div> <%= f.submit %> <% end %>
and set in routes file :root 'users#index' resources :users
i aded "gem 'bcrypt-ruby', '~> 3.1.2'" in gemfile
there 's no error shown render me new page
this shows server
started "/users/new" 127.0.0.1 @ 2014-10-13 16:03:52 +0100 processing userscontroller#new html [deprecated] i18n.enforce_available_locales default true in future. if want skip validation of locale can set i18n.enforce_available_locales = false avoid message. rendered users/new.html.erb within layouts/application (107.0ms) completed 200 ok in 201ms (views: 146.0ms | activerecord: 49.0ms) started post "/users" 127.0.0.1 @ 2014-10-13 16:04:09 +0100 processing userscontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"slnneso5r8eolj1fgcfi/aer59jjpkcsrp0vgkxzupw=", "user"=>{"f_name"=>"tdg", "l_name"=>"gdfghf", "pseudo"=>"tdrytr", "email"=>"teryte", "password_digest"=>"[filtered]", "password_confirmation"=>"[filtered]"}, "commit"=>"create user"} (1.0ms) begin (0.0ms) rollback rendered users/new.html.erb within layouts/application (12.0ms) completed 200 ok in 61ms (views: 48.0ms | activerecord: 1.0ms)
your problem validations password automatically added using has_secure_password, , failing validations on local machine trying sign simple password.
as practice please show errors model if maintain user on create new user page.
on user view, if don't have error output code, following:
<% if @user.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@user.errors.count, "error") %> prohibited user beingness saved:</h2> <ul> <% @user.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
also, debug seek see if output this:
@user.save
is true or not?
you in future:
saved = @user.save rails.logger.info("was user saved? #{saved}") if saved # redirect else # etc end
ruby-on-rails
No comments:
Post a Comment