2016-12-01 49 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

相關問題