Note that there are some explanatory texts on larger screens.

plurals
  1. POundefined method `[]' for nil:NilClass, on user model create
    primarykey
    data
    text
    <p>How do one handle facebook user that dont provide some value? I am using the koala gem and omniauth.</p> <p>I get the following error: </p> <pre><code>NoMethodError in SessionsController#create undefined method `[]' for nil:NilClass app/models/user.rb:14:in `block in from_omniauth' app/models/user.rb:5:in `tap' app/models/user.rb:5:in `from_omniauth' app/controllers/sessions_controller.rb:3:in `create' </code></pre> <p>And I get this error because the facebook user my app is trying to autenticate dosent have some data values that my app want, like location, education history etc..</p> <p>A new user process, beging in the SessionsController: </p> <pre><code>class SessionsController &lt; ApplicationController def create user = User.from_omniauth(env['omniauth.auth']) session[:user_id] = user.id redirect_to root_url, notice: "Signed in!" end end </code></pre> <p>Then in the user model:</p> <pre><code>def self.from_omniauth(auth) where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user| user.provider = auth["provider"] user.uid = auth["uid"] user.name = auth["info"]["name"] unless auth["info"].blank? user.first_name = auth["info"]["first_name"] unless auth["info"].blank? user.last_name = auth["info"]["last_name"] unless auth["info"].blank? user.image = auth["info"]["image"] unless auth["info"].blank? user.email = auth["info"]["email"] unless auth["info"].blank? user.gender = auth["extra"]["raw_info"]["gender"] if (!auth["extra"].blank? &amp;&amp; !auth["extra"]["raw_info"].blank?) user.location = auth["extra"]["raw_info"]["location"]["name"] if (!auth["extra"].blank? &amp;&amp; !auth["extra"]["raw_info"].blank? &amp;&amp; !auth["extra"]["raw_info"]["location"].blank?) user.token = auth["credentials"]["token"] unless auth["credentials"].blank? # highschool data user.highschool_name = auth["extra"]["raw_info"]["education"][0]["school"]["name"] if (!auth["extra"].blank? &amp;&amp; !auth["extra"]["raw_info"]["education"][0]["school"].blank?) user.highschool_year = auth["extra"]["raw_info"]["education"][0]["year"]["name"] if (!auth["extra"].blank? &amp;&amp; !auth["extra"]["raw_info"]["education"][0]["year"].blank?) # graduate school data user.graduateschool_name = auth["extra"]["raw_info"]["education"][1]["school"]["name"] if (!auth["extra"].blank? &amp;&amp; !auth["extra"]["raw_info"]["education"][1]["school"].blank?) user.graduateschool_year = auth["extra"]["raw_info"]["education"][1]["year"]["name"] if (!auth["extra"].blank? &amp;&amp; !auth["extra"]["raw_info"]["education"][1]["year"].blank?) user.add_friends user.save! user end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload