Question

I am playing with a simple app that does a Facebook login using omniauth-facebook. This is for learning purposes. I am able to get the login to work and I get the callback. After this I am trying to display the user's name, email in the show.html.erb but for some reason it always prints blank. HEre is what I am doing in the controller:

def create
  @c_user = User.from_omniauth(env["omniauth.auth"])
  @c_name = @c_user.name
  @c_email = @c_user.email
  @c_website = @c_user.website
  logger.debug("current user has name  #{@c_user.name}")
  redirect_to root_url, notice: "Signed in as #{@c_name} #{@c_email}!"
 end

This function prints the correct value for user name and email and the notice is also correctly sent to the show view. However in the show.html.erb, blank values are printed.

HEre is my show.html.erb:

<p id="notice"><%= notice %></p>

<p>
 <strong>Name:</strong>
 <%= puts @c_name, @temp_var %>
</p>

<p>
<strong>Email:</strong>
<%= @c_email %>
</p>

<p>
  <strong>Website:</strong>
  <%= @c_website %>
</p>

<%= link_to "Sign out", signout_path, id: "sign_out" %>
Était-ce utile?

La solution

It sounds like you're rendering show.html.erb from your root_url. Instead of doing that, you could have just rendered the show action and your view would have populated correctly.

The reason your variables are not available in the view is because you are doing a redirect. When you do a redirect none of the instance variables are available in the redirected url.

So, instead of redirecting, you could just render the show action as follows:

def create
  @c_user = User.from_omniauth(env["omniauth.auth"])
  @c_name = @c_user.name
  @c_email = @c_user.email
  @c_website = @c_user.website
  logger.debug("current user has name  #{@c_user.name}")

  flash[:notice] = "Signed in as #{@c_name} #{@c_email}!"
  render :show 
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top