1

Ruby on Rails アプリケーション (2.3.x) に取り組んでおり、ユーザーがログインまたは登録できるフォームを作成したいと考えています。これも同じ形でやりたい。次のようなフォーム要素を置き換える JS 関数があります。

ログインフォーム:

<% form_for @user do |f| %>
<div id="form">
    <%= f.label :email, "E-mail" %>
    <%= f.text_field :email %>

    <%= f.label :password, "Password" %>
    <%= f.password_field :password %>

    <%= link_to "I don't have an account, "#", :id => "changeForm"%>
    <%= f.submit "Login" %>
</div>
<% end %>

ID「changeForm」は、フォーム要素を変更する JS 関数をトリガーします。したがって、URL を押すと、html は次のようになります。

<% form_for @user do |f| %>
<div id="form">
    <%= f.label :name, "Name" %>
    <%= f.text_field :name %>

    <%= f.label :email, "E-mail" %>
    <%= f.text_field :email %>

    <%= f.label :password, "Password" %>
    <%= f.password_field :password %>

    <%= f.label :password_confirmation, "Password confirmation" %>
    <%= f.password_field :password_confirmation %>

    <%= link_to "I do have an account, "#", :id => "changeForm"%>
    <%= f.submit "Register" %>
</div>
<% end %>

ユーザー モデルに必要な検証を追加しました。

class User < ActiveRecord::Base
    attr_reader :password
    validates_presence_of :name, :email, :password
    validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
    validates_confirmation_of :password   

しかし、電子メール/パスワードを入力すると、名前が欠落しており、パスワード フィールドが確認されていないというエラーが表示されます。したがって、ユーザーモデルで次のような厄介なプログラミングを行うことができます。

#if password_conf or the name are present the user has tried to register...
if params[:user][:password_confirmation].present? || params[:user][:name].present?
   #so we'll try to save the user
   if @user.save
     #if the user is saved authenticate the user
     current_session.user = User.authenticate(params[:user])
     #if the user is logged in?
     if current_session.user.present?
       flash[:notice] = "succesvully logged
       redirect_to some_routes_path
     else
       #not logged in...
       flash[:notice] = "Not logged in"  
       render :action => "new"            
     end
   else
     #user not saved
     render :action => "new"
   end
 else
   #So if the params[:user][:password_confirmation] or [:user][:name] weren't present we geuss the user wants to login...
   current_session.user = User.authenticate(params[:user])
     #are we logged_in?
     if current_session.user.present?
       flash[:notice] = "Succesvully logged in"
       redirect_to some_routes_path
     else
       #errors toevoegen    
       @user.errors.add(:email, "The combination of email/password isn't valid")
       @user.errors.add(:password," ")
       render :action => "new"
     end
 end
end

検証がなければ、これは機能します(私見ダーティーコードであり、コントローラーにあるべきではありません)。しかし、私は validates_presence_of メソッドを使用したいのですが、「構成よりも規約」を平手打ちしたくありません。

だから私が試した別のことは、フォームに隠しフィールドを追加することです:

    #login form
    <%= f.hidden_field :login, :value => true %>
    # and ofcourse set it to false if we want to register.

そして、メソッドを使用したかった: before_validation

before_validation_on_create do |user|
  if params[:user].login == true #throws an error i know...
      validates_presence_of :email, :password
      validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  else
    validates_presence_of :name, :email, :password
    validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
    validates_confirmation_of :password        
  end
end

しかし、パラメータにアクセスできないため、これは機能しません。また、ログインはユーザー オブジェクトの属性ではありません。しかし、ユーザーがログインしたい場合、この方法で電子メールとパスワードのパラメーターを検証できると思いました。ユーザーが登録したい場合は、他のすべての属性。

だから私が考えることができるのは、私が望むようには機能しません。だから私の主な目標はこれです:

ユーザーモデルの検証メソッドを使用してログイン/登録するための1つのフォーム。したがって、ログインしたいが情報を入力しない場合 => 検証エラーが発生します。ユーザーがログインしたいが、電子メールとパスワードの組み合わせが一致しない場合は、"@user.errors.add(:email, "組み合わせがデータベースに見つかりませんでした...")" を返します。ユーザー登録も同様です...

前もって感謝します!

4

1 に答える 1

1

各フォームに適切なコントローラーアクションを指定する必要があります。

# Registration form
<% form_for @user, :url => users_path do |f| %>
...
<% end %>

# Login form, non-RESTful action
<% form_for @user, :url => login_users_path do |f| %>
...
<% end %>

loginこれは、にアクションがあることを前提としていますUsersControllerが、専用のコントローラーでセッションを管理する場合は、次のようにします。

# Use SessionsController (Sessions#Create) to create a new user session
<% form_for @user, :url => sessions_path do |f| %>
...
<% end %>
于 2011-01-03T06:49:37.650 に答える