我可以在不创建自己的一组视图的情况下自定义Devise的注册控制器吗?

时间:2016-12-01 20:18:53

标签: ruby-on-rails devise ruby-on-rails-5

我有app/views/devise次观看,我想要做的就是将一些实例变量传递给我的edit.html.erb

有没有办法让我这样做而无需将registrations/edit.html.erb移到另一个文件夹?

修改1

这就是我所做的。

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

这是我的routes.rb

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

这是我的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 %>

这是我的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>

但这是我得到的错误:

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

修改2

这是我的服务器日志,它似乎没有像我想要的那样向Users::RegistrationsController发送请求:

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)

我错过了什么?

1 个答案:

答案 0 :(得分:1)

您希望扩展variable = Application.InputBox(prompt:="Information?") 的默认行为,而无需移动文件。

我认为最简单的解决方案是将注册控制器继承到新类并覆盖使用的控制器。

首先创建一个新的控制器,让我们说Devise::RegistrationsController - &gt; RegistrationsController

继承app/controllers/registrations_controller.rb以保留所有行为和观看,并将您的代码添加到Devise::RegistrationsController下的edit操作。

super

然后将设计路线指向正确的注册控制器。

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

现在您应该可以在devise_for :users, :controllers => {:registrations => "registrations"} 内访问您的实例。

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

OP编辑

确保将app/views/devise/registrations/edit.html.erb指定到正确的路径:

devise_scope

这是基于我的版本被路由到控制器 devise_scope :user do get "settings", to: "users/registrations#edit" end 的事实。