Omniauth-Identity不创建用户

时间:2014-11-16 20:28:37

标签: ruby-on-rails ruby ruby-on-rails-4 omniauth

我正在尝试使用Omniauth-identity gem来创建用户,并且出于某种原因我遇到了会话错误。为什么身份甚至指向创建会话而不是创建用户?如何获取Identity以创建用户并将其重定向到正确的路径?

尝试注册新用户时出现

错误

No route matches {:action=>"feed", :controller=>"users", :id=>nil} missing required keys: [:id]

20 if user
21   session[:user_id] = user.id
22   redirect_to feed_user_path(@user), notice: "Signed in!"
23 else
24   session[:user_id] = nil
25   flash.now[:error] = 'Invalid email/password combination'

app/controllers/sessions_controller.rb:22:in `create'

的routes.rb

match '/signup',   to: 'users#new',       via: 'get'
match '/signin',   to: 'sessions#new',         via: 'get'
match "/auth/identity/callback", to: "sessions#create", via: 'post'

sessions_controller

def create
if omniauth_env = request.env.fetch("omniauth.auth", nil)
  email = request.env['omniauth.auth']['info']['email']
else
  email = params['session']['email']
end

user = Identity.find_or_create_by(email: email).user
Token.update_or_create_with_omniauth(user, omniauth_env) if omniauth_env

if user
  session[:user_id] = user.id
  redirect_to feed_user_path(@user), notice: "Signed in!"
else
  session[:user_id] = nil
  flash.now[:error] = 'Invalid email/password combination'
  render 'new'
end

users_controller

def create
  @user = User.new(user_params)
  if @user.save
    session[:user_id] = @user.id
    redirect_to callback_links_path
  else
    render 'new'
  end
end

new.html.erb(用户)

<%= form_tag("/auth/identity/register", method: 'post') do %>
  <% if @identity && @identity.errors.any? %>
    <div class="error_messages">
      <h2><%= pluralize(@identity.errors.count, "error") %> prohibited this account from being saved:</h2>
      <ul>
      <% @identity.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="row" id="content-box-signup">
    <div class="span4">  
      <div class="field">
        <%= label_tag :first_name %>
        <%= text_field_tag :first_name, @identity.try(:first_name) %>
      </div>
      <div class="field">
        <%= label_tag :last_name %>
        <%= text_field_tag :last_name, @identity.try(:last_name) %>
      </div>
      <div class="field">
        <%= label_tag :user_name %>
        <%= text_field_tag :user_name, @identity.try(:user_name) %>
      </div>
      <div class="field">
        <%= label_tag :email %>
        <%= text_field_tag :email, @identity.try(:email) %>
      </div>
      <div class="field">
        <%= label_tag :password %>
        <%= password_field_tag :password %>
      </div>
      <div class="field">
        <%= label_tag :password_confirmation %>
        <%= password_field_tag :password_confirmation %>
      </div>
      <br>
      <div class="actions"><%= submit_tag "Create my account", class: "btn btn-large btn-info" %></div>
    </div>
  </div>
<% end %>

new.html.erb(会话)

<div class="row" id="content-box-signup">
  <div class="span4">
  <%= form_tag "/auth/identity/callback" do %>
    <div class="field">
      <%= label_tag :auth_key, "Email" %>
      <%= text_field_tag :auth_key %>
    </div>
    <div class="field">
      <%= label_tag :password %>
      <%= password_field_tag :password %>
    </div>
    <div class="actions"><%= submit_tag "Login" %></div>
  <% end %>
    <p><%= link_to "Forgot password?", new_password_reset_path %></p>
    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

在sessions_controller中,您使用局部变量user而不是实例变量@user

当你再调用feed_user_path(@user)时,你用nil调用它,并且该方法缺少id。