Syntax Error Popping Up Trying to Add Friendly Id Gem


I am a beginner and I'm currently following Mackenzie Child's Portfolio + Blog Rails Youtube tutorials to deploy a website on Heroku. You can find the link to the tutorial here:

I'm trying to add the friendly id gem but am running into this problem in my terminal:

Ivys-MacBook-Pro:portfolio ivychen$ Post.find_each(&:save)
-bash: syntax error near unexpected token `&'

I don't know how to fix it. I think I might've not formatted the lines in my post.rb correctly, so here they are if you want to take a look:

class Post < ApplicationRecord
has_attached_file :image, styles: { large: "600x600>", medium: "300x300>", thumb: "150x150#"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/

extend FriendlyId
friendly_id :title, use: :slugged

This is what I've put in my post_controller.rb:

def find_post 
    @post = Post.friendly.find(params[:id])

This is probably an easy fix, but I just don't know how to do it! Thank you! Any help would be greatly appreciated.

Show source
| rubygems   | ruby-on-rails   | heroku   2017-01-02 19:01 2 Answers

Answers ( 2 )

  1. 2017-01-02 20:01

    Navigate to your rails project directory and run 'rails c' to enter the rails console where you can write ruby code with rails loaded up.

    /rails/myproject $ rails c
    Running via Spring preloader in process 81138
    Loading development environment (Rails
    irb(main):001:0> Post.find_each(&:save)

    You're trying to evaluate ruby/rails code in bash (terminal) when you need to be in the rails console.

  2. 2017-01-02 20:01

    You should run Post.find_each(&:save) in rails console rails c

◀ Go back