1

我很感谢有专家在场

我正在尝试使用 range_field 滑块来更新一些用户参数/整数。
当我提交表单时,参数会在 <%= @user.risk %> 文本字段中临时更新,但我没有保存到数据库中,当我重新加载时,更改就消失了。我大量借鉴 Michael Hartl 的 Rails 4.0 教程。

下面我最初使用了 form_for(@user) 但 current_user 似乎总体上工作得更好。`

 <%= form_for(current_user) do |f|%>

       <%= f.range_field :risk, :in=>0..100, :id=>"slider1"%>

      <%= f.range_field :tax, :in=>0..100, :id=>"slider2"%>

       <%= f.range_field :income, :in=>0..100, :id=>"slider3"%>

       <%= f.range_field :international, :in=>0..100,:id=>"slider4"%>
       <%= f.submit "Save Profile" %>
  <% end %>`

class UsersController < ApplicationController

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

  def new
   @user = User.new
  end

 def create
    @user = User.new(user_params)
  if @user.save
    sign_in @user
    flash[:success] ="Welcome"
    redirect_to @user
  else
    render 'new'
   end
  end

  def edit
  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

 private

    def user_params
      params.require(:user).permit(:name, :email, :password, 
:password_confirmation, :risk, :tax, :income, :international)
  end
end

class User < ActiveRecord::Base


 before_save :set_default
 before_save { self.email = email.downcase }
 before_create :create_remember_token
 validates :name, presence: true, length: { maximum: 50 }
 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
 validates :email, presence: true, format: { with: 
VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
 has_secure_password
 validates :password, length: { minimum: 6 }


def User.new_remember_token
  SecureRandom.urlsafe_base64
end

def User.encrypt(token)
  Digest::SHA1.hexdigest(token.to_s)
end

def set_default
   self.risk = "50"
   self.tax = "50"
   self.income = "50"
   self.international = "50"
end

  private

   def create_remember_token
     self.remember_token = User.encrypt(User.new_remember_token)
   end
end

在我尝试通过 range_field 更新参数并输入 User.find(4) 之后,控制台的输出

=> #<User id: 4, name: "Tony", email: "2@g.c", created_at: "2013-09-23 06:37:22",
updated_at: "2013-09-23 06:37:23", password_digest: "...", remember_token: "...", 
risk: 50, tax: 50, income: 50, international: 50> 

这是我本地 WEBRick Rails Server 的输出

Started PATCH "/users/1" for 127.0.0.1 at 2013-09-23 06:15:32 -0700
Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "user"=>{"risk"=>"70", "tax"=>"61", "income"=>"54", "international"=>"58"}, "commit"=>"Save Profile", "id"=>"1"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "1"]]
   (0.0ms)  begin transaction
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('2@g.c') AND "users"."id" != 1) LIMIT 1
   (0.0ms)  rollback transaction
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '.....' LIMIT 1
   (0.0ms)  begin transaction
  CACHE (0.0ms)  SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('2@g.c') AND "users"."id" != 1) LIMIT 1
   (0.0ms)  rollback transaction
  Rendered users/edit.html.erb within layouts/application (1.9ms)
  Rendered layouts/_header.html.erb (0.2ms)
Completed 200 OK in 8ms (Views: 6.0ms | ActiveRecord: 0.3ms)

非常感谢大家

4

1 回答 1

0

解决方案很简单

def update
   @user = User.find(params[:id])
   @user.update_attributes!(user_params)

......

于 2013-09-26T18:34:15.253 回答