Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails Getting name can't be blank error when trying to add new user
    primarykey
    data
    text
    <p>I am a Ruby on Rails newbie, and am trying to put into practice some of the tutorials I have been following.</p> <p>I am currently trying to get a basic user signup working.</p> <p>When the signup form is completed and submitted, the Firstname and Lastname fields are blanked out and I get two error messages (I also get a failing error when running the cucumber tests):</p> <ul> <li>Firstname can't be blank</li> <li>Lastname can't be blank</li> </ul> <p>I think I have missed something fairly obvious with the authentication code, but can't spot what I have missed.</p> <p>All my code is on my <a href="https://github.com/nstoker/ScoutHut" rel="nofollow">github account</a></p> <h2>Controller</h2> <pre><code>class UsersController &lt; ApplicationController before_action :set_user, only: [:show, :edit, :update, :destroy] # GET /users # GET /users.json def index @users = User.all end # GET /users/1 # GET /users/1.json def show end # GET /users/new def new @user = User.new end # GET /users/1/edit def edit end # POST /users # POST /users.json def create @user = User.new(user_params) respond_to do |format| if @user.save format.html { redirect_to @user, notice: 'User was successfully created.' } format.json { render action: 'show', status: :created, location: @user } else format.html { render action: 'new' } format.json { render json: @user.errors, status: :unprocessable_entity } end end end # PATCH/PUT /users/1 # PATCH/PUT /users/1.json def update respond_to do |format| if @user.update(user_params) 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 # DELETE /users/1 # DELETE /users/1.json def destroy @user.destroy respond_to do |format| format.html { redirect_to users_url } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_user @user = User.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def user_params params.require(:user).permit(:firstname, :middlename, :lastname, :email, :password, :password_confirmation) end end </code></pre> <h2>Model</h2> <pre><code>class User &lt; ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :middlename validates :firstname, :presence =&gt;true validates :lastname, :presence=&gt;true validates :email, :presence=&gt;true, :uniqueness =&gt; { :case_sensitive =&gt; false } before_validation :downcase_email def name [firstname, middlename, lastname].compact.join(' ') end private def downcase_email self.email = self.email.downcase if self.email.present? end end </code></pre> <h2>Views User</h2> <h1>new.erb.rb</h1> <pre><code>&lt;h1&gt;New user&lt;/h1&gt; &lt;%= render 'form' %&gt; &lt;%= link_to 'Back', users_path %&gt; </code></pre> <h2>_form.rb</h2> <pre><code>&lt;%= form_for(@user) do |f| %&gt; &lt;% if @user.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@user.errors.count, "error") %&gt; prohibited this user from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @user.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= f.label :firstname %&gt;&lt;br /&gt; &lt;%= f.text_field :firstname %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :lastname %&gt;&lt;br /&gt; &lt;%= f.text_field :lastname %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :middlename %&gt;&lt;br /&gt; &lt;%= f.text_field :middlename %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :email %&gt;&lt;br /&gt; &lt;%= f.text_field :email %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload