mercredi 18 octobre 2017

displaying rails index view files with a list from database

I have a rails index view file displaying a list of projects that have been created. I originally added some logic hoping that the page will display both with an empty projects table or a projects table with a populated database. However I am not getting the expected results.

index.html.erb

<div class="container">
  <div class="row">
    <div class="well projects">
      <h1>Projects</h1>
      <h2><%= link_to 'New Project', new_project_path, class: 'btn btn-default' %></h2>

      <table class="table table-striped">
        <% if Project.exists?(current_account) %>
          <% @project.each do |project| %>
            <tr>
              <td><%= link_to project.title, project_tasks_path(project) %></td>
              <td><%= project.details %></td>
            </tr>
          <% end %>
        <% end %>
      </table>
    </div>
  </div>
</div>

application_controller.rb

def current_account
  @current_account ||= User.find_by(subdomain: request.subdomain)
end

projects_controller.rb - index action

def index
    @project = current_account.user_projects
end

If I remove the code <% if Project.exists?(current_account) %> and try running the page with an empty projects table, I receive the following error:

NoMethodError in Users::Projects#index
undefined method `title'

If I run the page with the code <% if Project.exists?(current_account) %> it provides a depreciation warning in the console. Also, It doesn't populate the list as expected, In some cases not populating the records and leaving a blank list even though some exist.

Aucun commentaire:

Enregistrer un commentaire