Rails在更新用户详细信息时尝试创建用户

时间:2015-07-30 10:01:46

标签: ruby-on-rails ruby

我正在学习rails并使用网络指南构建身份验证系统,并遵循railscasts教程。

我现在已经站稳脚跟,如果可能的话需要一些帮助。

当我尝试编辑用户个人资料时,收到一条错误消息,告诉我由于已经收集了电子邮件和用户名等字段,因此无法创建帐户。

环顾四周似乎与我的编辑表单提交方式有关,但我无法解决!

任何帮助都将不胜感激。

Users_controller.rb

def edit
 @user = User.find(params[:id])
end

def update
  @user = User.find(params[:id])
  if @user.update_attributes(user_params)
  flash[:success] = "Profile updated"
  redirect_to @user
  else
    render 'edit'
  end
end

edit.html.erb          <%= form_for:user,url:' / users'做| f | %GT;

            <form class="m-t" role="form" action="#">
                <div class="form-group">
                  <%= f.label :name %>
                  <%= f.text_field :name, class: 'form-control', autocomplete: "off" %>
                </div>
                <div class="form-group">
                  <%= f.label :user_type %>
                  <%= f.select(:user_type, ['Admin', 'Technical', 'Accounts'], {}, { :class => 'form-control' }) %>
                </div>
                <div class="form-group">
                  <%= f.label :email %>
                  <%= f.text_field :email, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :telephone %>
                  <%= f.text_field :telephone, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :mobile %>
                  <%= f.text_field :mobile, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :user_name %>
                  <%= f.text_field :user_name, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :password %>
                  <%= f.password_field :password, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :password_confirmation %>
                  <%= f.password_field :password_confirmation, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :company_admin%>
                  <%= f.check_box :company_admin, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :user_admin %>
                  <%= f.check_box :user_admin, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :emergency_contact %>
                  <%= f.check_box :emergency_contact, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.submit "Submit", class: "btn btn-primary block full-width m-b" %>
                </div>
            </form>

            <% end %>

Rails记录

User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = 'emailaddress@gmail.com' LIMIT 1
User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE "users"."user_name" = 'AUserName' LIMIT 1

user.rb

class User < ActiveRecord::Base
 has_secure_password

 validates :name, presence: { message: "Please enter your name." }
 validates_uniqueness_of :email, presence: { message: "Please enter your email address." }
 validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]    {2,})\z/i, message: "Please enter a valid email address.", allow_blank: true
 validates :telephone, presence: { message: "Please enter your phone number." }
 validates :mobile, presence: { message: "Please enter your mobile number." }
 validates_uniqueness_of :user_name, presence: { message: "Please enter your user name." }
 validates_confirmation_of :password, presence: { message: "Please enter your password" }, allow_nil: true

 before_create { generate_token(:auth_token) }

 def generate_token(column)
  begin
   self[column] = SecureRandom.urlsafe_base64
  end while User.exists?(column => self[column])
 end

 def send_password_reset
  generate_token(:password_reset_token)
  self.password_reset_sent_at = Time.zone.now
  save!
  UserMailer.password_reset(self).deliver
 end

end

1 个答案:

答案 0 :(得分:0)

等我验证错了。刚发现它。

<%= form_for :user, url: '/users' do |f| %>

这不会使用@user对象,这意味着rails认为您正在尝试创建用户。

将其切换为

<%= form_for @user do |f| %>

Rails还会推断发布的正确位置,因此您不再需要url选项。