局部视图中的实例变量

时间:2015-06-10 21:11:12

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

我有一个导航栏是部分视图,我需要在设计页面上呈现,以便用户编辑他们的个人资料。事实上,我只有一个页面,但添加执行帐户维护的路径已经搞砸了我的导航栏加载,因为实例变量不存在。我怎样才能获得一个全局实例变量,无论如何都可以在我的导航栏中使用?

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

视图/设计/注册/ edit.html.erb

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

信息页/ _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

我在这里错过了什么让所有观点都有效?

谢谢!

1 个答案:

答案 0 :(得分:1)

好的,似乎Where is the controller of Devise ? how to add data from other models in users/edit page?很有帮助。我必须执行以下操作才能使这个工作部分用于设计视图:

function formValidation() { var fName = document.getElementById("Fname").value; var lName = document.getElementById("Lname").value; var email = document.getElementById("email").value; var pass = document.getElementById("password").value; var cpass = document.getElementById("cpassword").value; if (fName_validation(fName, 20, 3)) { if (lName_validation(lName, 20, 3)) { if (email_validation(email)) { if (pass_validation(pass, 20, 6)) { } } } } function fName_validation(fName, max, min) { var check_name = /^[A-Za-z\s ]{2,20}$/; if (!fName.match(check_name)) { alert("First name should not be empty / length be between " + max + " to " + min); fName.focus(); return false; } return true; } function lName_validation(lName, max, min) { var check_name = /^[A-Za-z\s ]{2,20}$/; if (!lName.match(check_name)) { alert("Last name should not be empty / length be between " + max + " to " + min); lName.focus(); return ; } return true; } function email_validation(email) { var checkk_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; if (!email.match(checkk_email)) { alert("Please enter a valid email!"); email.focus(); return ; } return true; } function pass_validation(pass, max, min) { var check_password = /(?=.*\d)(?=.*[a-z]).{6,}/; if (!pass.match(check_password)) { alert("Your password should have at least one number,one letter and should be a least 6 characters!"); pass.focus(); return false; } if (pass !== cpass) { alert("Password don't match!"); cpass.focus(); return false; } } alert("Well Done!You successfully registered!"); return true; }

controllers / users / registrations.rb 下创建了注册控制器,然后我能够添加我需要的实例变量rails g devise:controllers users -c registrations方法:

edit

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

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