循环遍历rails中的多个属性

时间:2021-03-29 20:19:24

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?

我在用户模型中使用has_many:技能,我想知道,在用户选择了动态生成的表单文件中的许多技能后,如何将所有技能提取到数组中并将其显示在我的视图中?

2 个解决方案

#1


1  

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

#2


2  

I think this is what you want to do... if not, please update your question with what output you're really looking for. 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>

#1


1  

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

#2


2  

I think this is what you want to do... if not, please update your question with what output you're really looking for. 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>