简体   繁体   English

循环遍历rails中的多个属性

[英]looping through multiple attributes in rails

I am using has_many: skills in my user model and I'm wondering, after a user has selected many skills in dynamically generated form fileds, how do I pull all the skills into an array and display them into my view?我在我的用户 model 中使用has_many: skills ,我想知道,在用户在动态生成的表单文件中选择了许多技能后,如何将所有技能拉入一个数组并将它们显示到我的视图中?

I think this is what you want to do... if not, please update your question with what output you're really looking for.我认为这就是你想要做的......如果不是,请用你真正想要的 output 更新你的问题。 Like @thefugal, I'm assuming skill has a name.像@thefugal 一样,我假设技能有一个名字。

Since @user.skills is an array, you need to loop over each of them.由于@user.skills 是一个数组,因此您需要遍历它们中的每一个。

<ul>
  <% @user.skills.each do |skill| %>
    <li><%= skill.name %></li>
  <% end %>
</ul>

Have the skills been persisted yet?技能有没有坚持下来? If so, with that association, you should be able to get an array of a user's skills (assuming you have a user @user ) with如果是这样,通过该关联,您应该能够获得一系列用户的技能(假设您有一个用户@user

@user.skills

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM