0

こんにちは私はform_forヘルパーを使用してフォームを作成した簡単なアプリケーションを持っています。送信時に、トリガーされたアクションでモデルオブジェクトにアクセスしようとすると、その中のすべての情報が正しく取得されません。一部の属性が空白になります。私はすでに問題のデバッグに数時間を費やしましたが、無駄です。どんな助けでも大歓迎です。Rails 3.2 Ruby 1.9.2

フォームのコードは:signup.html.erbです

<%= form_for (@user) , :url => { :action => "signup" ,:method=>"post"} do |u| %>
  <p>
    <%= u.label :login %>
    <%= u.text_field :login ,:size => 20 %>
  </p>
  <p>
    <%= u.label :password %>
    <%= u.text_field :password,:size => 20 %>
  </p>
  <p>
    <%= u.label :password_confirmation %>
    <%= u.text_field :password_confirmation,:size => 20 %>
  </p>
  <p>
    <%= u.label :email %>
    <%= u.text_field :email ,:size => 20 %>
  </p>
  <%= u.submit %>
<% end %>

user_controller.rbの「サインアップ」アクションのコードは次のとおりです。

 def signup


      @user = User.new(params[:user])

       if request.post?  

        if @user.save!

          session[:user] = User.authenticate(@user.login, @user.password)
          flash[:message] = "Signup successful"
          redirect_to :action => "index"          
        else

          flash[:warning] = "Signup unsuccessful"
        end
      end

    end

送信ボタンをクリックすると、次のエラーが発生します

 ActiveRecord::RecordInvalid in UserController#signup

Validation failed: Password can't be blank, Password doesn't match confirmation

Rails.root: /Users/kabir/ror/mmeter1
Application Trace | Framework Trace | Full Trace

app/controllers/user_controller.rb:42:in `signup'

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"yTC5i0HKltD4z40f0AQhir58CF9Pz+19VnKi3lvT6aE=",
 "user"=>{"login"=>"abcde",
 "password"=>"123456",
 "password_confirmation"=>"123456",
 "email"=>"abcde@kk.com"},
 "commit"=>"Create User",
 "method"=>"post"}

ユーザーのモデル検証は次のとおりです。

  validates_presence_of :login, :email, :password, :password_confirmation, :salt
  validates_uniqueness_of :login, :email
  validates_confirmation_of :password

送信直前のフォーム用に生成されたソースコードは

<form accept-charset="UTF-8" action="/user/signup?method=post" class="new_user" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="yTC5i0HKltD4z40f0AQhir58CF9Pz+19VnKi3lvT6aE=" /></div>
  <p>
    <label for="user_login">Login</label>
    <input id="user_login" name="user[login]" size="20" type="text" />
  </p>
  <p>
    <label for="user_password">Password</label>
    <input id="user_password" name="user[password]" size="20" type="text" />
  </p>
  <p>
    <label for="user_password_confirmation">Password confirmation</label>
    <input id="user_password_confirmation" name="user[password_confirmation]" size="20" type="text" />
  </p>
  <p>
    <label for="user_email">Email</label>
    <input id="user_email" name="user[email]" size="20" type="text" />
  </p>
  <input name="commit" type="submit" value="Create User" />
</form>

また、user_controllerのサインアップアクションに@userの値を記録したところ、次の値が表示されました。

The user params is {"login"=>"abcde", "password"=>"123456", "password_confirmation"=>"123456", "email"=>"abcde@kk.com"}
The user object is --- !ruby/object:User
attributes:
  id: 
  username: 
  password: 
  created_at: 
  updated_at: 
  login: abcde
  hashed_password: !binary |-
    MzFiNDk4MGRmMTU2OTEwOThhNDdkYzNjOTZhOTFjYWFiOTVkN2NiOA==
  email: abcde@kk.com
  salt: tgCV8xIfxS

ユーザー名とパスワードのフィールドが空白になっていることに注意してください。どんな助けでも大歓迎です。

これがモデルコードです

 class User < ActiveRecord::Base
 # prevent the following fields from being updated through a post
 # request. ie, these fields cannot be updated as a result of a form
 # submittion. They can only be updated from within the model itself.
  attr_protected :id, :salt
  attr_accessible :login,:password,:password_confirmation,:email

 # set the basic validation rules for the different attributes of the user
  validates_length_of :login, :within => 3..40
 #  validates_length_of :password, :within => 5..40
  validates_presence_of :login, :email, :password, :password_confirmation, :salt
  validates_uniqueness_of :login, :email
  validates_confirmation_of :password
 #  validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email"
4

2 に答える 2

0

You did not post your entire model (it's missing an end), are you sure you do not have additional code? The password has been stored in hashed_password and removed from the password and password_confirmation, and some method is doing that. That's because it is unsafe to use those two fields, because they are plaintext.

So, either in your model, or an included gem does alter the saving of a User, and that is breaking your validations.

Maybe you used code from this page?

http://www.napcsweb.com/blog/2010/03/11/basic-authentication-in-ruby-on-rails-in-case-you-forgot/

And hint: in your form use password_fields instead of text_fields...

于 2012-08-10T11:46:15.070 に答える