首页 > 解决方案 > 将变量放入 Rails 部分

问题描述

只是一个简单的问题,我是 Rails 上的新蜜蜂,我想知道如何在 Rails 中使用 partials,比如我们如何与 react 中的组件交互,而 rails 只是一个简单的例子。

下面是我尝试过的代码。我想渲染自定义导航栏

<% @nav_links = ['hello','welcome','sign up']
              @showDropdown = false
              @isAuth = false            
            %>
            <%= render 'shared/navlinks' , locals: {nav_links: @nav_links, showDropdown: @showDropdown , isAuth: @isAuth }  %>```

I tried the above code to pass the variables into a partial but is there a way like the below

```rubyonrails
<%= render 'shared/navlinks' ,
 locals: {nav_links: ['hello','welcome','sign up'], 
showDropdown: false , 
isAuth: false }  %>```



I am getting `undefined local variable or method` I feel like I am missing something here is this how the instance variable works in ruby
? Any suggestion is much appreciated... Thank you

标签: ruby-on-railspartials

解决方案


只需替换为

 <%= render partial: 'shared/navlinks' , locals: {nav_links: ['hello', 'welcome', 'signup'], showDropdown: false, isAuth: false }  %>

无需设置成变量


推荐阅读