[Review] When creating a web application with Rails, syntax error, unexpected')', expecting => ...]}% ","% # {params [: content]}% "]) ...

Hi, this is shirakaba.

This time I would like to review Rails.

I've erased the work I was making at the online school before, so I'm thinking of making it again as a review.

However, the content I write this time has solved the error that occurred in the process of making the application! I would like to write that.

As an aside, I've been chased by something else for a long time and forgot about my important things, my original purpose, and what I enjoyed, so I went back to the starting point and learned the task and programming in parallel. I want to do it while doing it!

I will learn as I like!

※important point Inexperienced and now a beginner studying on his own. It is written for the purpose of outputting what you have learned. Therefore, I think there may be mistakes, but in that case, I would appreciate it if you could see it with warm eyes.

What was solved

What did you try to do?

When I tried to create a search function and tried to create a search action on the controller, I made the following description, but the following error occurred.

def search
  if params[:content].present?
     @book = Book.where(user_id: current_user.id,['title LIKE ? OR author LIKE ? OR label LIKE ?',"%#{params[:content]}%", "%#{params[:content]}%", "%#{params[:content]}%"])
  else
    @book = Book.none
  end
end

Error statement

When I access the URL, the usual error screen appears ...

syntax error, unexpected ')', expecting => ...]}%", "%#{params[:content]}%"]) ... ^
@book = Book.where(user_id: current_user.id,['title LIKE ? OR author LIKE ? OR label LIKE ?',"%#{params[:content]}%", "%#{params[:content]}%", "%#{params[:content]}%"])

In the terminal

SyntaxError (/Folder name/Folder name/controllers/books_controller.rb:6: syntax error, unexpected ')', expecting =>


...]}%", "%#{params[:content]}%"])
...                              ^
):

Solutions

From the conclusion, I solved it by writing where in two and connecting them.

@book = Book.where(user_id: current_user.id).where('title LIKE ? OR author LIKE ? OR label LIKE ?',"%#{params[:content]}%", "%#{params[:content]}%", "%#{params[:content]}%")

This time it was an error due to a description error, but I had a hard time finding it ... If you investigate the cause properly and think calmly, it may not have taken so long.

Although it is a review, it makes me realize that there is still a lack of training. Besides, this may not be a beautiful way of writing, so I'll investigate it more properly.

Finally

In the future, I would like to be able to output any trivial or embarrassing things in this way. Then ~

Recommended Posts

[Review] When creating a web application with Rails, syntax error, unexpected')', expecting => ...]}% ","% # {params [: content]}% "]) ...
[Rails] syntax error, unexpected tSTRING_END, expecting'''
[Rails] Avoid SELECT issued when creating a model with belongs_to defined!
[Rails] Creating a new project with rails new
Build a web application with Javalin
Naming convention when creating a new controller or model with rails
Creating a java web application development environment with docker for mac part1
Create a simple web application with Dropwizard
Rails Basics of creating a new application
[Rails] Error resolution when generating tokens with PAYJP
Error memorandum that occurred when creating a CI / CD environment [Rails + CircleCI + Capistrano + AWS]
A story that stumbled when deploying a web application created with Spring Boot to EC2
Error encountered with notes when deploying docker on rails
A story stuck with an error when building Vagrant + Virtulbox CentOS7.8 and creating a shared folder