Note that there are some explanatory texts on larger screens.

plurals
  1. POSyntax error in model and controller after setting up Paperclip Gem
    text
    copied!<p>I'm burning through the onemonthrails tutorial. And I've just installed the paperclip gem and set up the model with some validations. I thought I followed the tut exactly but when I go into the localhost:3000/pins I get this weird syntax error that points to both the model and the controller. I didn't have this problem before the paperclip install...</p> <p>Here's the error:</p> <pre><code>SyntaxError (C:/Sites/code/omrails/app/models/pin.rb:7: syntax error, unexpected '}', expecting tASSOC): app/controllers/pins_controller.rb:9:in `index' </code></pre> <p>Here's the github branch, if you go into the master you can see the code before I installed paperclip (when it was working fine):</p> <p><a href="https://github.com/justuseapen/omrails/tree/error" rel="nofollow">https://github.com/justuseapen/omrails/tree/error</a></p> <p>EDIT Here's the offending code from the model:</p> <pre><code>class Pin &lt; ActiveRecord::Base validates :description, presence: true validates :user_id, presence: true validates_attachment :image, presence: true content_type: {content_type['image/jpeg','image/jpg','image/png','image/gif']} size: {less_than: 5.megabytes } belongs_to :user has_attached_file :image, styles: {medium:"320x240"} end </code></pre> <p>And from the controller:</p> <pre><code> class PinsController &lt; ApplicationController before_filter :authenticate_user!, except: [:index] before_action :set_pin, only: [:show, :edit, :update, :destroy] # GET /pins # GET /pins.json def index @pins = Pin.all end # GET /pins/1 # GET /pins/1.json def show end # GET /pins/new def new @pin = current_user.pins.new end # GET /pins/1/edit def edit @pin=current_user.pins.find(params[:id]) end # POST /pins # POST /pins.json def create @pin = current_user.pins.new(pin_params) respond_to do |format| if @pin.save format.html { redirect_to @pin, notice: 'Pin was successfully created.' } format.json { render action: 'show', status: :created, location: @pin } else format.html { render action: 'new' } format.json { render json: @pin.errors, status: :unprocessable_entity } end end end # PATCH/PUT /pins/1 # PATCH/PUT /pins/1.json def update @pin=current_user.pins.find(params[:id]) respond_to do |format| if @pin.update(pin_params) format.html { redirect_to @pin, notice: 'Pin was successfully updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @pin.errors, status: :unprocessable_entity } end end end # DELETE /pins/1 # DELETE /pins/1.json def destroy @pin=current_user.pins.find(params[:id]) @pin.destroy respond_to do |format| format.html { redirect_to pins_url } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_pin @pin = Pin.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def pin_params params.require(:pin).permit(:description, :image) end end </code></pre> <p>SOLUTION:</p> <p>Ok. So I'm not sure what the problem was, but I rewrote the code like so and it worked:</p> <pre><code> validates :description, presence: true validates :user_id, presence: true has_attached_file :image, styles: { medium: "320x240&gt;"} validates_attachment :image, presence: true, content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }, size: { less_than: 5.megabytes } belongs_to :user end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload