Reputation: 45
everybody. Forgive any mistakes in my English since it's my second language. Also only my second question here.
I'm learning Ruby on Rails and I have a very basic question, but sadly wasn't able to find a specific answer anywhere.
So, all I did is a simple scaffold (rails g scaffold book title author status:integer), nothing more, and I'm studying everything it does. That's all.
The thing is I don't understand why in the index view the "link_to" helper uses "book" as argument instead of "@books" for both Edit and Destroy links.
Since in the show view the same "link_to" uses @book for Edit.
At first, I thought they could be interchangeable, but they aren't, since when I change the "@", Rails throws an error. I'm clearly missing something basic here.
Where does this difference in both views come from?
Thank you all.
Upvotes: 2
Views: 121
Reputation: 379
In your view show @book
is a variable that was declared in your controller and that contains a collection of books. This collection is traversed through the for_each method, on each tour you get a book that is saved in a variable | book |
That book
is an instance of your @books
collection and link_to
receives it to build the link.
In your edit view @book is a variable declared in your controller. You may not be clear why that variable is declared within a method invoked with a before action.
the @ is used to define an @instance_variable
Upvotes: 3