我有我的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)
我錯過了什麼?
這正是我所做的,但它不起作用。等一下,我會用我的代碼更新這個問題並告訴你。 – marcamillion
我敢肯定,這應該工作,我以前做過。目前無法測試,但稍後會進行測試。 – fbelanger
查看更新的問題。 – marcamillion