css - 局部 View 中的实例变量

标签 css ruby-on-rails ruby twitter-bootstrap devise

我有一个导航栏,它是一个局部 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/

相关文章:

HTML div 被部分覆盖

javascript - toggleClass 不保留以前的点击事件 jquery

ruby-on-rails - 用于从 Ruby on Rails 应用发出 HTTP 请求的传出端口

ruby-on-rails - Rails 上的链接 ruby​​ 查询返回那些不为零的值

HTML 电子邮件缺少附件

jquery - 从 html 中的父 css 类转义

jquery - 想要在旋转时显示元素的厚度

ruby-on-rails - 我应该在Heroku上对Puma使用preload_app吗?

ruby-on-rails - 我该如何解释这个 Rails/PostgreSQL 错误?

ruby - 产量 :content doesn't show content_for :content