gpt4 book ai didi

css - 局部 View 中的实例变量

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:08 27 4
gpt4 key购买 nike

我有一个导航栏,它是一个局部 View ,我需要在设计页面上呈现它,以便用户编辑他们的个人资料。事实上,我只有一个页面,但是添加执行帐户维护的路径搞乱了我的导航栏加载,因为实例变量不存在。无论如何,我如何才能获得适用于我的导航栏的全局实例变量?

application_helper.rb

def get_company
@company = current_user.company
end

def get_locations(company)
@locations = if @company
current_user.company_locations.order(:name)
else
[]
end
end

pages_controller.rb

def home
@company = get_company
@locations = get_locations(@company)
@reports = if @company
@company.reports.order("created_at DESC").limit(5)
else
[]
end
end

views/devise/registrations/edit.html.erb

<%= render partial: "pages/navigation" %>
... devise form ....

pages/_navigation.html.erb

<li class="right-menu-item">
<button class="btn dropdown-toggle " type="button" id="dropdownMenu1" data-toggle="dropdown">
<% if @company %>
<%= @company.name %>
<% else %>
<%= current_user.email %>
<% end %>
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu1">
<% @locations.each do |location| %>
<li role="presentation">
<%= link_to root_path(location_id: location.id), role: "menuitem", tabindex: "-1" do %>
<%= image_tag "check-green.svg" if location == @location %>
<span><%= location.name %></span>
<% end %>
</li>
<% end %>
</ul>

问题所在: enter image description here

要使它在所有 View 中正常工作,我在这里缺少什么?

谢谢!

最佳答案

好吧,似乎是Where is the controller of Devise ? how to add data from other models in users/edit page?很有帮助。我必须执行以下操作才能在部分设计 View 中完成这项工作:

rails g devise:controllers users -c registrations

它在 controllers/users/registrations.rb 下创建了一个注册 Controller ,然后我能够添加我需要的实例变量来执行 edit 方法:

def edit
@company = get_company
@locations = get_locations(@company)
end

还必须稍微更改 route.rb 文件以方便新 Controller :

devise_for :users, skip: [:sessions],
controllers: {
registrations: 'users/registrations'
}

关于css - 局部 View 中的实例变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30767385/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com