2016-12-01 39 views
1

我有我的app/views/devise的意见,我想要做的就是能够传递一些实例变量到我的edit.html.erb我是否可以自定义Devise的注册控制器而无需创建自己的视图集?

有没有办法让我这样做,而不必将我的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

您想扩展Devise::RegistrationsController的默认行为,而不移动文件。

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

首先创建一个新的控制器,比如说RegistrationsControllerapp/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"} 

现在您应该可以在app/views/devise/registrations/edit.html.erb之内访问您的实例。

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

OP编辑

确保指定devise_scope正道:

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

这是基于这样的事实,我的版本被路由到控制器users/registrations

+0

这正是我所做的,但它不起作用。等一下,我会用我的代码更新这个问题并告诉你。 – marcamillion

+0

我敢肯定,这应该工作,我以前做过。目前无法测试,但稍后会进行测试。 – fbelanger

+0

查看更新的问题。 – marcamillion

相关问题