I'm new to rails and i'm trying to do a simple project for school.
I have generated a scaffold as follows to begin.
rails generate scaffold Book title:string content:text code:string
What I'm trying to do, is to display a list of the books id's in a div on the same page (localhost:3000/books) when I click on a link. I tried to follow this procedure but it doesn't seem to work, nothing is visibly rendered on the page when I click the link at the end of the /books page
Here is my books/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Books</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Code</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @books.each do |book| %>
<tr>
<td><%= book.title %></td>
<td><%= book.content %></td>
<td><%= book.code %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Book', new_book_path %>
<br>
<%= link_to 'All books', books_path, remote: true %>
<br>
<br>
<div id="content" >
</div>
my books_controller's index method
def index
@books = Book.all
respond_to do |format|
format.html
format.js
end
end
the _books partial
<% @books.each do |book| %>
<div class="book">
<%= book.id %>
</div>
<% end %>
and the books/index.js.erb
$("#content").html("<%= j (render 'books') %>");
When I click on the link that should render the partial, I get this in the terminal and nothing is displayed on the page.
Started GET "/books" for 127.0.0.1 at 2017-05-12 17:35:02 +0200
Processing by BooksController#index as JS
Rendering books/index.js.erb
Book Load (0.3ms) SELECT "books".* FROM "books"
Rendered books/_books.html.erb (2.4ms) [cache miss]
Rendered books/index.js.erb (3.9ms)
Completed 200 OK in 8ms (Views: 6.0ms | ActiveRecord: 0.3ms)
I imagine I am missing something, but I really can't figure out why. Any ideas? thank you
PS: Rails version: 5.1.0 and ruby 2.4.0