Answer the question
In order to leave comments, you need to log in
Explain how to deal with the application controller?
Hello.
I already asked a similar question, but did not receive an answer. I took the RailsForZombies course at Codeschool. I studied the ruby guide, now I'm reading several books. I practice, of course. But I can't understand one thing. Please do not send me to learn further, but simply explain how it works and how to solve the problem.
The problem is this. I need to display the values of the Books (name) and Users (first_name) tables in the application.html.erb view. I understand that all other controllers inherits from Application Controller. And that for the application controller you need to register data loading and outputting them to the view. I tried to do the same as in books_controller, it doesn't work. First_name is undefined, then user or book.
Please show on this example how to solve the problem with a little explanation. I really want to understand how to solve this so that in the future there will be no problems. Thanks in advance.
Answer the question
In order to leave comments, you need to log in
Before displaying data, books, or a user, the controller receives them from the database. Usually the url contains the id of the object. Like books/65/edit. These 65 will go into params[:id], and in the controller it will be written @book = Book.find(params[:id]).
Therefore, in the ApplicationController you need to define the values of the variables that you want to use in the view. At the same time, you cannot get IDs from the URL and you need to come up with some other way to decide which book and user you want to display. Or maybe take everyone out.
For views application_helper.rb
For general functions application_controller.rb
For model data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question