Ruby on Rails中的用户注册表单损坏

时间:2016-11-07 20:59:20

标签: ruby-on-rails ruby database forms ruby-2.3.1

我在Ruby on Rails中遇到了用户注册表单的问题。 只需使用用户名,电子邮件和密码,我就可以使用它。但是,当我尝试添加名字,姓氏和邮政编码的字段时,它会停止将任何数据插入数据库,然后重定向回到注册表单。

我以正确的方式生成迁移并迁移数据库 - 表单仍然无效。

然后我完全删除了数据库,并在包含所有字段的情况下重新创建它,但它仍然无效。使用正确的字段正确生成数据库,我不确定断开连接的位置。

我正在使用ruby 2.3.1。

用户控制器:

class UsersController < ApplicationController

  def new
  @user = User.new
end

def create 
  @user = User.new(user_params) 
  if @user.save 
    session[:user_id] = @user.id 
    redirect_to '/home' 
  else 
    redirect_to '/register' 
  end 
end

private
  def user_params
    params.require(:user).permit(:username, :email, :password, :location, :fname, :lname)
  end

end

用户模型:

class User < ApplicationRecord

    has_secure_password 

end

用户/新表格:

  <div class="container">      
  </div>

<div id="contact" class="container-fluid bg-grey">

  <h2 class="text-center">Create An Account</h2>
  <form>

        <%= form_for(@user) do |f| %>
        <%= f.text_field :username, :placeholder => "Username" %>
      <%= f.email_field :email, :placeholder => "Email" %>
      <%= f.password_field :password, :placeholder => "Password" %><br/><br/>
        <%= f.text_field :fname, :placeholder => "First Name" %>
        <%= f.text_field :lname, :placeholder => "Last Name" %>
        <%= f.number_field :location, :placeholder => "ZIP Code (US Only)" %><br/><br/>
      <%= f.submit "Create an Account", class: "btn-submit" %>
    <% end %>

</div>

 </ul>
</nav>
</body>
</html>

创建用户迁移:

class CreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      t.string :username
      t.string :email
      t.string :password_digest
      t.numeric :location
      t.string :fname
      t.string :lname

      t.timestamps
    end
  end
end

来自日志:

 Started GET "/register?utf8=%E2%9C%93&authenticity_token=b6M1gpQx8BTLhBqjjh6R%2BlSGghcSh18Mg7eGskLGNyq11RBvGCX%2BOJjVhSLwwRmZunn1sYvV4MGHB7vrJw6Rhg%3D%3D&user%5Busername%5D=jsmith1&user%5Bemail%5D=jsmith%40email.com&user%5Bpassword%5D=[FILTERED]&user%5Bfname%5D=John&user%5Blname%5D=Smith&user%5Blocation%5D=60606&commit=Create+an+Account" for ::1 at 2016-11-07 15:14:50 -0600
Processing by UsersController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"b6M1gpQx8BTLhBqjjh6R+lSGghcSh18Mg7eGskLGNyq11RBvGCX+OJjVhSLwwRmZunn1sYvV4MGHB7vrJw6Rhg==", "user"=>{"username"=>"jsmith1", "email"=>"jsmith@email.com", "password"=>"[FILTERED]", "fname"=>"John", "lname"=>"Smith", "location"=>"60606"}, "commit"=>"Create an Account"}
  Rendering users/new.html.erb within layouts/application
  Rendered users/new.html.erb within layouts/application (2.0ms)
Completed 200 OK in 195ms (Views: 148.1ms | ActiveRecord: 21.5ms)

)
    Completed 200 OK in 760ms (Views: 714.8ms | ActiveRecord: 3.5ms)

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我明白了。额外的表单标签弄乱了表单提交。一旦我从Ruby表单代码上方删除<form>,它就可以了。感谢回复的人。