Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance Variables Not Working in Device Controller - Rails 4

I could not word the title properly but I hope you understand. I have followed the exact example on how to generate a controller.

rails generate devise:controllers users

My routes:

devise_for :users, controllers: { sessions: "users/sessions" }

Then copy (new.html.erb) from devise/sessions to views/users/sessions, then delete the view from devise/sessions/

Then in:

class Users::SessionsController < Devise::SessionsController
  def new
     super
    @foo = 'Bar'
  end
end

Now in my views/users/sessions/new.html.erb:

<%= @foo %> # should show Bar

That not showing. Am I missing something?

like image 261
Sylar Avatar asked Dec 11 '25 19:12

Sylar


1 Answers

This is what your SessionsController should look like:

class Users::SessionsController < Devise::SessionsController
  def new
    @foo = 'bar'
    super
  end
end

Note how super is below your custom code.

When you call super, it calls the parent method; in our case Devise::SessionsController#new. You need to initialize @foo before calling that method and rendering the new.html.erb.

like image 53
Max Wofford Avatar answered Dec 13 '25 17:12

Max Wofford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!