gpt4 book ai didi

ruby-on-rails - 设置 Paperclip Gem 后模型和 Controller 中的语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:37 26 4
gpt4 key购买 nike

我正在学习 onemonthrails 教程。我刚刚安装了回形针 gem 并通过一些验证设置了模型。我以为我完全遵循了 tut,但是当我进入 localhost:3000/pin 时,我得到了这个指向模型和 Controller 的奇怪语法错误。在回形针安​​装之前我没有遇到这个问题...

这是错误:

SyntaxError (C:/Sites/code/omrails/app/models/pin.rb:7: syntax error, unexpected

'}', expecting tASSOC):

app/controllers/pins_controller.rb:9:in `index'

这是 github 分支,如果你进入 master 你可以看到我安装回形针之前的代码(当它工作正常时):

https://github.com/justuseapen/omrails/tree/error

编辑这是模型中有问题的代码:

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

从 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

解决方案:

好的。所以我不确定问题出在哪里,但我像这样重写了代码并且它起作用了:

    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

最佳答案

你在不应该有逗号的地方多了一个逗号。

validates_attachment :image, presence: true,

应该是

validates_attachment :image, presence: true

关于ruby-on-rails - 设置 Paperclip Gem 后模型和 Controller 中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19122132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com