[英]Syntax error in model and controller after setting up Paperclip Gem
I'm burning through the onemonthrails tutorial. 我正在通过onemonthrails教程。 And I've just installed the paperclip gem and set up the model with some validations. 我刚刚安装了paperclip gem并使用一些验证设置了模型。 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. 我认为我完全遵循了tut但当我进入localhost时:3000 /引脚我得到了这个奇怪的语法错误,指向模型和控制器。 I didn't have this problem before the paperclip install... 在回形针安装之前我没有遇到这个问题...
Here's the error: 这是错误:
SyntaxError (C:/Sites/code/omrails/app/models/pin.rb:7: syntax error, unexpected
'}', expecting tASSOC):
app/controllers/pins_controller.rb:9:in `index'
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): 这是github分支,如果你进入master,你可以在我安装paperclip之前看到代码(当它工作正常时):
https://github.com/justuseapen/omrails/tree/error https://github.com/justuseapen/omrails/tree/error
EDIT Here's the offending code from the model: 编辑以下是该模型的违规代码:
class Pin < 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
And from the controller: 并从控制器:
class PinsController < 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
SOLUTION: 解:
Ok. 好。 So I'm not sure what the problem was, but I rewrote the code like so and it worked: 所以我不确定问题是什么,但是我重写了代码,并且它有效:
validates :description, presence: true
validates :user_id, presence: true
has_attached_file :image, styles: { medium: "320x240>"}
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
You have a comma where there shouldn't be one. 你有一个逗号,不应该有一个逗号。
validates_attachment :image, presence: true,
should be 应该
validates_attachment :image, presence: true
validates_attachment :image, presence: true
w \\ o','
You have a syntactical errors per the documentation on validation. 根据验证文档 ,您有语法错误。 You should be passing an array of strings to your content_type
validation: 您应该将一个字符串数组传递给content_type
验证:
content_type: ['image/jpeg','image/jpg','image/png','image/gif']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.