简体   繁体   English

语法错误,意外的'\\ n',期望::或'['或'。

[英]syntax error, unexpected '\n', expecting :: or '[' or '.'

I am trying to have an image uploader on my app using Paperclip, and my only problem is that I keep getting an Undefined Method error. 我正在尝试使用Paperclip在我的应用上安装图像上传器,而我唯一的问题是我不断收到未定义方法错误。 I have it set up on github, https://github.com/BBaughn1/savagelysaving 我在github上进行了设置, https://github.com/BBaughn1/savagelysaving

My User Controller: 我的用户控制器:

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

Then my Show.html.erb file: 然后是我的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>

In my post.rb file: 在我的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

in my development.rb file: 在我的development.rb文件中:

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

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

end and posts controller: 结束和发布控制器:

 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

There is no avatar attribute in your user model but there is an image attribute in your post model. 用户模型中没有化身属性,但帖子模型中有图像属性。

If you remove below code from user model and move it to post model, the image upload will work 如果您从用户模型中删除以下代码,然后将其移至后期模型,则可以上传图片

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

This should fix the bug. 这应该修复该错误。

I looked at your github to see if you made the same mistake I did. 我查看了您的github,看看您是否犯了与我相同的错误。 I think you forgot to add imagemagick to your development.rb file. 我认为您忘记将imagemagick添加到您的development.rb文件中。 When you install imagemagick on your local machine, it need to see where it's installed; 在本地计算机上安装imagemagick时,需要查看其安装位置。 which convert where show where. which convert哪里显示哪里。 It might look like this Paperclip.options[:command_path] = "/usr/local/bin/" if not, just copy what it show you and edit what's in the quotes " ". 如果不是这样的话,它看起来像是Paperclip.options[:command_path] = "/usr/local/bin/" ,只需复制显示的内容并编辑引号“”。

I hope that helps I know I was having some trouble getting it to work. 我希望这对我有所帮助,但是我知道我在将其投入使用时遇到了一些麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM