I would like to explain about the ** controller ** of Rails application.
** "Knowing about MVC model" ** ・ ** "About the role of controller" ** ・ ** "Practical usage" **
This is one of the ideas for managing code well when creating an application. It's just one way of thinking, so it's not absolutely correct. Rails applications incorporate this MVC framework.
** "Model" ** ・ ** "View" ** ・ ** "Controller" **.
・ Model
・ ・ ・ Responsible for business logic in the system
・ View
・ ・ ・ Processing such as display and input / output
・ Controller
・ ・ ・ Controls Model and View based on user input
** "Controller" ** receives a request from a client
For ** "Controller" **, ask ** "Model" ** for data processing.
** "Model" ** processes data from the database and returns the data to ** "Controller" **
** "Controller" ** asks you to process the data received in ** "View" **
** "View" ** creates a form (HTML document) suitable for the client to see and returns it to ** "Controller" **
** "Controller" ** returns the HTML text to the client as a response
By repeating 1 to 6, it is established as a Web application.
** Conclusion ** -Execute the ** action method ** of the specified controller based on the information sent from the routing. ・ ** Link with Model / View ** to return a response to the client
This method describes the specific processing content of the action specified from the routing.
Image of action method
class SamplesController < ApplicationController
def index
#Specific processing content
......
......
end
end
In this example ...
When the index action of the samples controller is specified in the routing, describe the action method to be executed in the form of def index ... end
.
Creating, modifying, or retrieving a database through a model.
Instruct the model using the methods of the ActiveRecord class that the model inherits. Below are some examples of methods.
Method | Contents |
---|---|
all | Get all the data in the database |
find | Get one piece of data in the database |
new | Generate new data creation |
save | Save data in database |
Method image
(Model class name).[Method]
Sample.all
Passing the data received from the model to the view and creating the response data to return to the client.
Assign instance variables to the data from the model.
@samples = Sample.all
This makes the data available in the view file.
In the ʻapp / controllersdirectory Set the
controller name_controller.rb` file.
Basically ** "plural" **. The reason is that it automatically guesses the files used by Rails by name. It could be singular, but the developer would have to explicitly write the code and it would cause an unexpected error.
name | Example | Remarks |
---|---|---|
Controller name | samples | |
Controller class name | SamplesController | Camel case |
Controller file name | samples_controller.rb | Snake case |
The contents of the controller file look like this.
app/controllers/samples_controller.rb
class SamplesController < ApplicationController
def index
#Specific processing content
......
......
end
end
The created controller file inherits ** "ApplicationController class" **.
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
end
ʻThe class defined in the app / controllers / application_controller.rb` file. It's a (almost) empty class that just inherits from the ** ActionController :: Base class **.
In principle, each controller inherits from the ApplicationController class It is used when common application functions (login / logout, etc.) are required.
A class that provides the basic functionality of the controller. It is responsible for the basic part related to request / response processing.
The controller gets the parameters of the HTTP request from the client via ** "params method" **.
Rails implicitly processes ** request parameters ** and stores the data in params in hash format. There is no difference in how data is received even when accessed by ** GET request / POST request **.
Access the web server by including the parameter in ** "Request URI" ** of the request line of the HTTP request. You can pass parameters in ** "URL format" ** and ** "query format" **.
When the following routing settings are made
Prefix Verb URI Pattern Controller#Action
user GET /users/:id(.:format) users#show
If you access with the URL http://sample.com/users/7
The : id
parameter of the request means 7
In Rails, it is stored in params and the value of params [: id]
is " 7 "
.
app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
params[:id] # "7"
end
end
At the end of the URL, write "?" Followed by "parameter name = value".
If there are multiple parameters, connect them with "&" and describe them.
Using the same routing in the example above
When you access with the URL http://sample.com/users/7?keyword=query&num=10
The value of params [: keyword]
is " query "
, and params [: num]
is " 10 "
.
app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
params[:keyword] # "query"
params[:num] # "10"
end
end
This is a request that occurs when the client presses the "Submit" button when inputting from the form. Access the web server by including the parameters in the ** "body" ** of the HTTP request.
You can also send more parameters than a GET request.
When saving "params" in the database, use a mechanism called ** "Strong Parameters" **.
It is a mechanism to eliminate the vulnerability of "Mass Assignment" that was used up to Rails3 series. In "Mass Assignment", params information about saving was described in ** model **. In "Strong Parameters", describe the information of params to be saved in ** controller **.
Strong_Image of Parameters
class UsersController < ApplicationController
def create
@user = User.new(user_params) # user_Create new data using params method
@user.save #Save to database via model
end
#Strong Parameters are not called from outside the class, so write them in a private method.
private
def user_params
params.require(:user).permit(:name, :age)
end
end
In this example
In the ʻuser_params method, the parameters to be saved are specified in
: nameand
: age of
: user`.
Specify the table name to be saved in the database.
Specify the column name to be saved in the database table.
That is all for the explanation of the controller. Thank you very much.
Recommended Posts