2011-01-02 7 views
1

私はRuby on Railsアプリケーション(2.3.x)で作業しています。ユーザがログインまたは登録できるフォームを作成したいと思います。私は同じ形でこれをやりたい私はこのようなフォーム要素を置き換えるJS機能を持っている:ログインまたは登録(Ruby on rails)

ログインフォーム:

<% 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 %> 

私は私のユーザモデルにneccesary検証を追加しました:

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 

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

私が考えることができるのは、どのように動作させたいのですか?だから私の主な目標はこれです:

1ユーザモデルの検証方法を使用してログイン/登録するためのフォーム。だから、もしログインしたいのであれば、どんな情報も記入しないでください=>妥当性検査のエラーを出します。また、ユーザーがログインしたいが、電子メールとパスワードの組み合わせが一致しない場合は、 "@ user.errors.add(:email、"その組み合わせはデータベース内に見つかりませんでした。ユーザー登録にも同じことが言えます...

ありがとうございます!

+0

なぜ同じルートでこれをやりたいのですか? – stef

+1

私はStefに同意します。同じページにユーザーのログインや登録を行うことはできますが、フォームを別のコントローラーやアクションに送信することができます。 –

答えて

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 %> 

これは、あなたのUsersControllerlogin作用を有すると仮定していますが、専用のコントローラーがあなたのセッションを管理したいのですが場合:

# Use SessionsController (Sessions#Create) to create a new user session 
<% form_for @user, :url => sessions_path do |f| %> 
... 
<% end %> 
+1

良い答えですが、将来は 'sessions/path'などのより簡単なルーティングヘルパーを使用するようにユーザーに指示してください。これらは' config/routes.rb'で自分自身で生成する必要があります。 –

+0

良い提案。私の答えを更新しました。 –

+0

ありがとうございました!男の男私は本当に今は馬鹿だと感じています...しかし、8時間をコーディングした後、あなたはエラーを作り始めています... –

関連する問題