lundi 19 octobre 2015

Rails - Getting the latest entry for a user from a table

I am having a table named names in which there is a column named name .The users will enter a text before proceeding to next page.

In the next page i want to show them the data they entered on previous page.

In controller i tried something like @names = current_user.names.all

In views i did

<ul class="names-list">
    <% @names.each do |nname| %>

        <li>
          <%= link_to nname.name, edit_name_path(nname) %>
        </li>

    <% end %>
  </ul>

and show the last list item for the latest date. But surely there will a Rails shortcut to fetch the last data for the user. Any help? Thanks in advance.

UPDATED : Now i did something like @last_name = current_user.names.last in controller and in view <%= @last_name.to_s %> and it is generates #<Name:0x007fae23ee1b68> instead of name string. @Lanny @Stewart

Aucun commentaire:

Enregistrer un commentaire