marcamillion
marcamillion

Reputation: 33755

Can I customize Devise's Registration Controller without creating my own set of views?

I have my app/views/devise views, and all I want to do is be able to pass a few instance variables into my edit.html.erb.

Is there a way for me to do that without having to move my registrations/edit.html.erb to another folder?

Edit 1

This is what I have done.

app/views/users/registrations_controller.rb:

class Users::RegistrationsController < Devise::RegistrationsController
  def edit
    if current_user_subscribed?
      @plan = Stripe::Plan.retrieve(current_user.plan_name)
    end
    super
  end
end

This is my routes.rb:

  devise_for :users, controllers: {
    invitations: 'invitations',
    registrations: 'users/registrations'
    },
    path_names: { :sign_up => "register",
                  :sign_in => "login",
                  :sign_out => "logout",
                  :settings => "settings" }

This is my app/views/devise/registrations/edit.html.erb:

<% if current_user_subscribed? %>
  <div class="col-md-12 subscribed-card">
    <%= render partial: "subscriptions/card_brand", locals: { brand: current_user.card_brand, plan: @plan } %>
  </div>
<% end %>

This is my app/views/subscriptions/_card_brand.html.erb

<div class="payment-card">
    <div class="row">
      <h2>Subscribed To: <%= plan.name %> for <%= formatted_price(plan.amount) %></h2>
    </div>
</div>

But this is the error I am getting:

NoMethodError at /settings
undefined method `name' for nil:NilClass

Edit 2

This is my server log, which doesn't seem to be sending the request to Users::RegistrationsController like I would want:

Processing by Devise::RegistrationsController#edit as HTML
  User Load (3.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 7], ["LIMIT", 1]]
  Rendering devise/registrations/edit.html.erb within layouts/devise
  Role Load (3.4ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'coach') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 7]]
  School Load (1.6ms)  SELECT "schools".* FROM "schools" WHERE "schools"."school_type" IN (1, 2)
  Rendered subscriptions/_card_brand.html.erb (12.2ms)
  Rendered devise/registrations/edit.html.erb within layouts/devise (904.5ms)
Completed 500 Internal Server Error in 994ms (ActiveRecord: 35.1ms)

What am I missing?

Upvotes: 1

Views: 2609

Answers (1)

fbelanger
fbelanger

Reputation: 3568

You want to expand on the default behaviour of the Devise::RegistrationsController, without moving the files around.

The easiest solution in my opinion would be to inherit the registrations controller into a new class and override the controller used.

Start by creating a new controller, let's say RegistrationsController -> app/controllers/registrations_controller.rb.

Inherit the Devise::RegistrationsController to keep all behaviours and views and add your code to edit action under super.

class RegistrationsController < Devise::RegistrationsController
  def edit
    super
    @my_instance = "Hello World"
  end
end

Then point devise's routes to the correct registrations controller.

devise_for :users, :controllers => {:registrations => "registrations"}

Now you should have access to your instance within app/views/devise/registrations/edit.html.erb.

https://gist.github.com/kinopyo/2343176

OP Edit

Make sure to specify the devise_scope to the right path:

  devise_scope :user do
    get "settings", to: "users/registrations#edit"
  end

Which is based on the fact that my version gets routed to the controller users/registrations.

Upvotes: 2

Related Questions