- Read Tutorial
- Watch Guide Video
As a continuation from the previous lesson, we are going to add CRUD functionality into our tasks_controller.rb
that we created without a scaffold in the last guide.
Though you can copy much of this code from projects_controller.rb
, I wouldn't recommend it because when you type from scratch you can better understand why you're putting each line of code, and it will also help you to understand the whole system better.
In the line, just after the class definition, make the before_action
call:
before_action :set_task, only: [:show, :edit, :update, :destroy]
With this code, I'm asking the before_action
method to call set_task
only for the show
, edit
, update
and destroy
methods.
Now, we have to add some methods that we don't already have. So, go to the bottom of the file and add the set_task
method, and make sure its private.
private def set_task end
Next, we'll go to the new
method where I have to create a new instance of Task
:
def new @task = Task.new end
Next, let's go to the create
method. In this method, I'm going to create a task variable that will take task_params
as its parameter. The next part is a respond_to
block as I want my application to respond to both html and json files. Also, I want it conditional, so I can check if the file is saved properly or not. This is all in the code below.
def create @task= Task.new (task_params) respond_to do |format| if @task.save format.html {redirect_to @task, notice: "Task was created successfully!"} format.json{render :show, status: :created, location: @task} else format.html {render :new} format.json {render json: @task.errors, status: :unprocessable_entity} end end
As mentioned, this code checks if the task is saved and there are no validation errors, and if so, it redirects back to the task and sends an alert message to the browser, in the case of html files. As for the json format, the same is passed as different parameters.
If the file is not saved for any reason, I want to redirect the user to a new form in the case of html. But, in the case of json, I want the system to display the error messages, so you what is wrong. When I put the status as unprocessable_entity
, json interprets it and displays the appropriate message.
This is what all the above code looks like:
Next, let's create our update method. This is similar to the create method, but will have a few changes.
If you see, the update method does not need a call to Task.new
obviously because we are not creating anything new here. Also, the @task.create
is changed to @task.update
and the html message is changed accordingly. In the else
block, we want our user to get redirected to the edit page again in case of an error, and this is why we have render :edit
. The rest is all the same.
The last method that we need to create is the destroy
method.
Here, we are deleting the method in the first line, and want html to display appropriate notice. Also, we want to redirect user to the corresponding project id. Since we deleted our task, it no longer exists, so we want the user to go to the project associated with the task. In json, we need no specific content.
In the next lesson, we'll talk about the code in set_task
and task_params
.