简体   繁体   中英

Invoking Rails Partials

So I understand how partial works and how useful they can be. I'm currently working my way through a screencast on building a web app and even though he says not to worry if you don't understand, I must understand regardless.

This is the controller and the action for index

class BooksController < ApplicationController
  before_action :authenticate_user!, only: [:new, :edit, :create, :update, :destroy]
  before_action :set_book, only: [:show, :edit, :update, :destroy]

  # GET /books
  # GET /books.json
  def index
    @books = Book.where(availability: true)
  end

and this is the corresponding index.html.erb

<p id="notice"><%= notice %></p>

<div class='row'>

  <%= render @books %>

</div>

<%= link_to 'New Book', new_book_path %>

I have a partial in the books view called _books.html.erb

Now the thing I'm unsure about is the @books = Book.where(availability: true)

I understand that @books is the instance variable for the index action, but what exactly is Book.where(availability: true) ? I the parameter being passed, but I'm still lost on Book. Is that referencing the model? I've read through the Action Controller overview over at guides.rubyonrails repeatedly and I'm still lost on this one singular point and it's driving me crazy and slightly frustrated that something that simple isn't clicking.

How exactly is the partial _books.html.erb being called? If I understand it correctly, def index tells rails to search my books view for index.html.erb correct?

Hmmm typing this out (and still trying to find the answer for myself) has me thinking I have it all wrong.

The partial is used for styling purposes in this instance correct? This is my _books.html.erb partial

  <div class='col-md-3'>
<div class='thumbnail'>
    <%= link_to book_path(book) do %>
    <%= image_tag 'book.jpg', class: 'img-responsive', alt: 'Pretty Bird'  %>
    <% end %>
  <div class='caption'>
    <h3><%=link_to book.name, book_path(book) %></h3>
    <h4><%= book.author %></h4>
    <p><%= book.description %></p>
  </div>
</div>

@books = Books.xxxx is like attr_reader correct? So the render @book in the index.html.erb is basically saying, fetch all stored book data from the user and display them via the _book partial, but only the ones whose availability equal true. Is this accurate? And I assume the stored data is sitting in development.sqlite3 within my db folder and that I can view this data via the rails console right? God I hope I'm on the right track at least.

If so, I guess my question ultimately is how does rails know to use the partial?

Any direction would be greatly appreciated!

When you call

@books = Book.where(availability: true)

So here books table in database which has been mapped with the help of ActiveRecord model Book.

books table has records with unique id's and fields you defined on Model are mapped with columns in database table. availability is the column in table.

So above query will call books table and fetch all the books where availability is 1 or true

This is Object relational mapping Book is the model which represent a table in database named as books,

ORM is Object Relational Mapper. It means you don't have to manually call the database yourself, the ORM handles it for you.

Ruby on Rails uses one called ActiveRecord

<%= render @books %> 

which is like

<%= render partial: 'books', collection: @books, locals: {name: "ruby"} %> 

which means that render the partial which should be named as _books which should be in books views else you can also specify the path render partial: 'users/books'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM