【问题标题】:syntax error, unexpected '\n', expecting :: or '[' or '.'语法错误,意外的 '\n',需要 :: 或 '[' 或 '.'
【发布时间】:2015-09-12 22:46:53
【问题描述】:

我正在尝试使用 Paperclip 在我的应用程序上安装图像上传器,我唯一的问题是我不断收到未定义方法错误。 我已经在 github 上设置好了,https://github.com/BBaughn1/savagelysaving

我的用户控制器:

class UserController < ApplicationController
    def create
      @user = User.create( user_params )
    end

    def destroy
        @user.image = nil
        @user.save
    end

    private

    # Use strong_parameters for attribute whitelisting
    # Be sure to update your create() and update() controller methods.

    def user_params
      params.require(:user).permit(:image)
    end
end

然后是我的 Show.html.erb 文件:

    <div id="post_content">
  <h1 class="title">
    <%= @post.title %>
  </h1>

  <p class="date">
    Submitted <%= time_ago_in_words(@post.created_at) %> Ago
    <% if user_signed_in? %>
      | <%= link_to 'Edit', edit_post_path(@post) %>
      | <%= link_to "Delete", post_path(@post), method: :delete, data: { confirm: 'Are you sure?' } %>
    <% end %>
  </p>

  <p class="body">
    <%= @post.body %>
    <%= image_tag @post.image.url(:medium) %>
  </p>

  <div id="comments">
    <%= render 'disqus' %>
  </div>
</div>

在我的 post.rb 文件中:

class Post < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  validates :title, presence: true, length: { minimum: 5 }
  validates :body, presence: true

  attr_accessor :image

  has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100#" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

在我的 development.rb 文件中:

Rails.application.configure do
  ***STUFF***

  Paperclip.options[:command_path] = "/usr/local/bin/"

结束 和职位控制器:

 class PostsController < ApplicationController
  # before_action :find_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @posts = Post.all.order('created_at DESC')
  end

  def new
    @post = Post.new
    attr_accessor :image
  end

  def create
    attr_accessor :image 
    @post = Post.new(post_params)
    if @post.save
      redirect_to @post
    else
      render 'new'
    end
  end

  def show
    @post = Post.find(params[:id])
  end

  def edit
    @post = Post.find(params[:id])
  end

  def update
    @post = Post.find(params[:id])
    attr_accessor :image
    if @post.update(params[:post].permit(:title, :body, :image))
      redirect_to @post
    else
      render 'edit'
    end
  end

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

    redirect_to root_path
  end

  private
    def post_params
      params.require(:post).permit(:title, :body, :image)
    end
end

【问题讨论】:

    标签: ruby-on-rails ruby imagemagick paperclip erb


    【解决方案1】:

    您的用户模型中没有 avatar 属性,但您的帖子模型中有 image 属性。

    如果您从用户模型中删除以下代码并将其移动到发布模型,则图像上传将起作用

    has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
    

    这应该可以修复错误。

    【讨论】:

    • 所以我这样做了,但出现了一个奇怪的错误。我收到一条消息说“发布模型缺少'image_file_name'所需的attr_accessor”所以我把它放在那里,它给了我这个:语法错误,意外'\ n',期待::或'['或'。'我试图用谷歌搜索问题,但没有任何效果
    • 您是否运行了 rake db:migrate?更新您的代码并推送到 repo 并让我知道它何时完成,我会检查它有什么问题。
    • 执行“rake db:migrate”命令后仍有错误。它发布在 github 上,这是我的创建定义: def create (at)post = Post.new(post_params) attr_accessor for 'image_file_name' if (at)post.save redirect_to (at)post else render 'new' end end
    • 你让它工作了吗,我从你的 master 分支得到了最新的代码,我可以运行没有任何错误。
    • 不(很抱歉这篇文章迟到了,我上过学(11 年级)然后工作),但我正在尝试
    【解决方案2】:

    我查看了您的 github,看看您是否犯了与我相同的错误。我认为您忘记将 imagemagick 添加到您的 development.rb 文件中。当你在本地机器上安装 imagemagick 时,它需要查看它的安装位置; which convert哪里显示哪里。它可能看起来像这样Paperclip.options[:command_path] = "/usr/local/bin/",如果不是,只需复制它向您显示的内容并编辑引号“”中的内容。

    我希望这有助于我知道我在让它工作时遇到了一些问题。

    【讨论】:

    • 我一开始就这样做了。我想我应该提到它,对不起。我将编辑我的问题
    猜你喜欢
    • 1970-01-01
    • 2019-12-09
    • 2022-02-25
    • 1970-01-01
    • 2021-03-10
    • 2012-01-14
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多