Ruby on rails Rails回形针-无法正确上载图像

Ruby on rails Rails回形针-无法正确上载图像,ruby-on-rails,view,model,controller,paperclip,Ruby On Rails,View,Model,Controller,Paperclip,我正在制作一个小的rails应用程序,需要能够将图像上传到post/ad,然后显示该图像 我正在使用“回形针”gem来处理图像上传。 每当我创建post/ad时,都会显示默认的“missing.jpg”,而不是我的图像 Ads控制器 class AdsController < ApplicationController before_action :set_ad, only: [:show, :edit, :update, :destroy] before_action :auth

我正在制作一个小的rails应用程序,需要能够将图像上传到post/ad,然后显示该图像

我正在使用“回形针”gem来处理图像上传。 每当我创建post/ad时,都会显示默认的“missing.jpg”,而不是我的图像

Ads控制器

class AdsController < ApplicationController
  before_action :set_ad, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!

  # GET /ads
  # GET /ads.json
  def index
    @ads = Ad.all
  end

  # GET /ads/1
  # GET /ads/1.json
  def show
  end

  # GET /ads/new
  def new
    @ad = Ad.new
  end

  # GET /ads/1/edit
  def edit
  end

  # POST /ads
  # POST /ads.json
  def create
    @ad = Ad.new(ad_params)

    respond_to do |format|
      if @ad.save
        format.html { redirect_to @ad, notice: 'Ad was successfully created.' }
        format.json { render action: 'show', status: :created, location: @ad }
      else
        format.html { render action: 'new' }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /ads/1
  # PATCH/PUT /ads/1.json
  def update
    respond_to do |format|
      if @ad.update(ad_params)
        format.html { redirect_to @ad, notice: 'Ad was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /ads/1
  # DELETE /ads/1.json
  def destroy
    @ad.destroy
    respond_to do |format|
      format.html { redirect_to ads_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_ad
      @ad = Ad.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def ad_params
      params.require(:ad).permit(:title, :url, images_attributes: [:preview])
    end
end
类AdsController
广告表单视图

<style type="text/css">
.header {
  display: none!important;
}
</style>

<%= form_for @ad, :html => {:multipart => true} do |f| %>
  <% if @ad.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@ad.errors.count, "error") %> prohibited this ad from being saved:</h2>

      <ul>
      <% @ad.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <span>Contact (URL, Mobile, Address)</span><br>
    <%= f.text_field :url %>
  </div>
  <div class="field">
    <%= f.label :image %><br>
    <%= f.file_field :preview %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
<h1>Listing ads</h1>




    <% @ads.each do |ad| %>
    <div class="adspace_grid">
      <%= image_tag ad.preview.url %>
      <div class="text_info">
      <span class="bold"><%= ad.title %></span> / 
      <span class="bold">Contact : </span><%= ad.url %>
      </div>
    </div> 
    <%= link_to 'Delete', ad, :method => :delete %> 
    <% end %>


<br>

<%= link_to 'New Ad', new_ad_path %>

.标题{
显示:无!重要;
}
{:multipart=>true}do | f |%>
禁止保存此广告:

联系人(URL、手机、地址)

广告模式

class Ad < ActiveRecord::Base

    attr_accessible :title, :url, :preview
    belongs_to :user
  has_attached_file :preview, :default_url => "missing.jpg"

  validates :title, length: { maximum: 20 }
  validates :url, length: { maximum: 20 }
end
class-Ad“缺少.jpg”
验证:标题,长度:{最大值:20}
验证:url,长度:{最大值:20}
结束
广告索引视图

<style type="text/css">
.header {
  display: none!important;
}
</style>

<%= form_for @ad, :html => {:multipart => true} do |f| %>
  <% if @ad.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@ad.errors.count, "error") %> prohibited this ad from being saved:</h2>

      <ul>
      <% @ad.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <span>Contact (URL, Mobile, Address)</span><br>
    <%= f.text_field :url %>
  </div>
  <div class="field">
    <%= f.label :image %><br>
    <%= f.file_field :preview %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
<h1>Listing ads</h1>




    <% @ads.each do |ad| %>
    <div class="adspace_grid">
      <%= image_tag ad.preview.url %>
      <div class="text_info">
      <span class="bold"><%= ad.title %></span> / 
      <span class="bold">Contact : </span><%= ad.url %>
      </div>
    </div> 
    <%= link_to 'Delete', ad, :method => :delete %> 
    <% end %>


<br>

<%= link_to 'New Ad', new_ad_path %>
列出广告
/ 
联系人:
:删除%>

任何帮助都将不胜感激。谢谢

编辑: 这是谷歌浏览器控制台中的错误。

问题在于您在哪里允许
ad
属性:

# Never trust parameters from the scary internet, only allow the white list through.
def ad_params
  params.require(:ad).permit(:title, :url, images_attributes: [:preview])
end
我不知道你为什么在这里有
images\u属性:[:预览]
。方法定义应为:

def ad_params
  params.require(:ad).permit(:title, :url, :preview)
end
您没有获得上传的图片并获得缺少的默认
。jpg
的原因是,表单中选择的图像文件没有创建,因为您不允许在
广告参数中使用
:preview

更新:

就路径而言,您应该能够提供其他参数,如
url
path
已附加文件
。大致如下:

# app/models/ad.rb

has_attached_file :preview, :default_url => "missing.jpg", 
                  :url  => "/assets/ads/preview/:id/:style/:basename.:extension",
                  :path => ":rails_root/public/assets/preview/:id/:style/:basename.:extension"

这样,您上传的所有预览文件都将位于
public/assets/ads/preview/:id/:style/
目录中

谢谢!这个参数肯定是个问题,但现在每当我做广告时,都不会显示图像,但当我检查图像是否存在时,通过谷歌开发者控制台,我可以导航到图像。所以它确实存在,但由于某些原因没有显示。请尝试为您的图像设置大小。看看这里:嗯,我刚刚做了,但它似乎没有任何效果。你验证了上传的图像和它的路径吗?你能从你的操作系统文件浏览器打开这个图像吗?另外,通过检查图像的src属性来检查生成的URL是否正确。是的,我已经验证了路径,并且它链接到了我试图使其可见的文件。在我的rails代码中,我已经设置了视图,但是当我查看html代码时,视图没有出现。