Understanding the View Table Display of Rails Table Results

Asked 1 years ago, Updated 1 years ago, 69 views

I'm a beginner.
If you want to display the results in view from the results obtained from the model,
If the contents of the model become variable (the process of changing the model with connection each time)
Some get the table @a aa, while others get the table b.
 
In the case of this book, the result is 1 view per controller, so there are many descriptions that specify the result of the line and output it.

<%@ado|a|%>
      <tr>
       <td><%=a.id%>/td>
        <td><%=a.title%></td>
      </tr>
    <%end%>
    </table>

How do I get this dynamically because the table that pulls it is different as above, so if I specify a column name, it will not be reflected in the view?

Or if there is a website that you can refer to, I would appreciate it if you could let me know.

ruby-on-rails ruby rails-activerecord

2022-09-30 21:18

1 Answers

You can use the class method columns to obtain column names, etc. (a.class.columns).

<%@a.each do|a|%>
  <%a.class.columns.map(&:name).each do|n|%>
    <%=a.send(n)%>
  <%end%>
<%end%>

Other Possible Methods:

  • If only some columns are different and the column does not exist, use try (a.try(:title))
  • If the model is different, put it in a different variable


2022-09-30 21:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.