2012-03-10 36 views
1

長時間讀者首次使用的用戶。 我把我一起先RoR應用程序和我隔絕一切我的應用程序應該使用到: -魔法和簡單的形式實現

  • 法術
  • Omniauth
  • 慘慘
  • Twitter的引導(轉換爲SASS)

和簡單的形式。

乾淨,清晰,簡單....不是。

不能爲我的生活整合(看起來是最簡單的任務)簡單的形式與巫術「登錄」,而不會在'remember_me'字段上得到錯誤。

簡單的表單沒有simple_form_tag(只有simple_form_for)選項,它最適合來自會話控制器新方法的登錄表單。相反,我必須在該方法中創建@user實例,但在'remember_me'字段中找到錯誤'undefined method`remember_me'「

任何幫助將不勝感激。

我的意思是非常好!巨大的thanx提前:)

sessions/new.html.erb 

<% provide :title, "Log in" %> 
<h1>Log in</h1> 
<%= simple_form_for @user, :html => { :class => 'form-horizontal' } do |f| %> 
    <fieldset> 
    <legend>Login</legend> 

    <%= f.input :email, input_html: { :maxlength => 100 } %> 
    <%= f.input :password, input_html: { :maxlength => 20 } %> 
    <%= f.input :remember_me, as: :boolean %> 


    <div class="form-actions"> 
     <%= f.submit nil, :class => 'btn btn-primary' %> 
     <%= link_to 'Cancel', users_path, :class => 'btn' %> 
    </div> 
    </fieldset> 
<% end %> 

    class SessionsController < ApplicationController 
    def new 
    @user = User.new 
    end 

回答

1

documentation說:

form_tag(url_for_options = {}, options = {}, &block) 
Starts a form tag that points the action to an url configured with url_for_options just 
like ActionController::Base#url_for. The method for the form defaults to POST. 

這表明form_tag旨在將數據直接發送到另一個URL,通過一個控制器動作處理。事實上,在RailsTutorial signin form中,Michael Hartl使用form_for函數而不是form_tag

form_for(:session, url: sessions_path) 

基本上,這個想法是,你要發送到以某種方式的控制器處理數據,而不是寫入數據庫。由於我們沒有用戶會話的模型,因此我們必須使用:session符號而不是@session,並告訴表單應將數據發送到哪個(哪個URL)。

我懷疑,雖然我不確定,但這也應該與simple_form_for一起使用。喜歡的東西:

<%= simple_form_for(:session, url: sessions_path) do |f| %> 

更新:我成功地改變了示例應用程序使用simple_form_for創建新的用戶會話的reference implementation

也就是說,假設Sessions控制器正在處理您的登錄,並且它有一個響應POST的方法(可能是create)。該控制器操作是您應該處理Sorcery登錄的位置。

如果您好奇,下面是關於form_for的Rails文檔。

+0

關於如何使用omniauth-identity的任何想法?我試過'code'simple_form_for(:user,url:「/ auth/identity/register」)'/ code' – gittinOld 2012-03-31 03:44:06

+0

關於如何使用這與omniauth身份?我試過'simple_form_for(:用戶,網址:「/身份證/身份證/註冊「)'。 user_controller'def new @user = env ['omniauth.identity'] || User.new end'。 model/user.rb'class User gittinOld 2012-03-31 03:51:11

+0

如果可以,請嘗試刪除您的第一條評論,因爲它沒有像你期望的那樣經歷。我不知道爲什麼這樣可以正常工作,並給你Heroku上的路由錯誤。作爲調試步驟,您應該在本地和Heroku上運行'rake routes'。我真的建議你把這個問題作爲一個新問題發佈,讓他人有機會在他們幫助你時贏得全部聲譽。如果有幫助,也可以點贊所選的答案。 – jrhorn424 2012-03-31 20:52:34

0

我試圖做到這一點,並陷入困境。看來這應該起作用,但是當我提交登錄表單時它會導致錯誤。以下是我的表單的樣子:

<h1>Log in</h1> 

<%= simple_form_for(:session, url: sessions_path) do |f| %> 

    <%= f.error_notification %> 

    <%= f.input :email %> 
    <%= f.input :password %> 
    <%= f.input :remember_me, :as => :boolean %> 

    <div class="form-actions"> 
    <%= f.submit "Login", :class => 'btn btn-primary' %> 
    </div> 
<% end %> 

<%= link_to 'Forgot Password?', new_password_reset_path %> 

不知道如何使這項工作。我調試輸出表示形式試圖發送正確的數據:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
utf8: ✓ 
authenticity_token: wGdDEmG91p7RHzWZKLGgMQvKD+XupS1Z557vNDDG6GM= 
session: !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
    email: [email protected] 
    password: test 
    remember_me: '1' 
commit: Login 
action: create 
controller: sessions 

另外,I「m如果這個錯誤在控制檯:

gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms) 

但我在routes.rb文件有resources :sessions