Accessing And Traversing Array Fields In Views In Rails
TAGS:
Postgres Mysql Column Array Rails
How To Access And Traverse Array Fields In Views In Rails
In our example, We suppose that you watched our tutorials about rails application with postgres database or rails application with Mysql database and adding column type array to postgres table via migration only and adding column type array to postgres table via model and migration and adding column type array to mysql table.
Then, You must add the fields or columns
into permited params
in our example reader
and editor
in the bottom of its controller
, in our example PostsController
as:
def post_params
params.require(:post).permit(:name, :comment,:reader,:editor)
end
Then, Edit the show
page to be like:
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @post.name %>
</p>
<p>
<strong>Comment:</strong>
<%= @post.comment %>
</p>
<p>
<strong>Readers:</strong>
<ol>
<% @post.reader.each do |reader| %>
<li><%= reader %></li>
<% end %>
</ol>
</p>
<p>
<strong>Editors:</strong>
<ol>
<% @post.editor.each do |editor| %>
<li><%= editor %></li>
<% end %>
</ol>
</p>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
Note, You can use full array functions
in ruby
with the reader
and editor
fields.
Now, Take a look to this tutorial video.