Question

I was using rails to create a sqlite table in my application, I tried an example from a tutorial which I typed the command:

rails generate scaffold Product \
     title:string description:text image_url:string price:decimal

It worked and generated a table, however, when I ran

rake db:migrate

It came up some problem:

SyntaxError in ProductsController#index
products_controller.rb:72: syntax error, unexpected ':', expecting ')' ...rams.require(:product).permit(: title, :description, :image_... ... ^ 

So I looked into the table and found that there's an extra space between title and string, it's like this:

title : string
description :text
image_url :string
price :decimal

Then I changed it and the corresponding controller. The controller was like this before I changed it, also an extra space here:

 def product_params
      params.require(:product).permit(: title, :description, :image_url, :price)
    end

After that, whenever I tried to create a table, the same problem came again and again. The first attribute always has an extra space while the following attributes don't. So anyone knows what's the problem? Thank you so much! :)

Was it helpful?

Solution

Might be a tad late, but remove the \ so that the rails generator looks like:

rails generate scaffold Product title:string description:text image_url:string price:decimal

The \ is used to denote the end of a line in a multiline command and when you use it, hit enter to continue writing on the next line. (^ on windows).

If you've already created the database you can rebuild it using:

rails generate scaffold Product title:string description:text image_url:string price:decimal --force

If like me you found that code in Agile Web Development for Rails 4; the preceding paragraph explains the \.

Hope that small roadblock didn't push you away from rails all those months ago!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top