尝试使用Paperclip gem在我的Rails视图中显示图像时,我遇到了一个问题。每当我尝试使用<%= image_tag @event.image.url %>
渲染存储在我的数据库中的图像时,allI get就是消息&#34; Graph返回错误。必须使用Active访问令牌来查询有关当前用户的信息&#34;。
必须注意三件事: 1)我是Ruby on Rails中的菜鸟。 2)我的应用程序中没有用户模型或任何access_token要求。 3)所有其他数据在视图中渲染得很好,显然包括图像在内的所有数据都正确地传递到数据库中。
控制器:
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
# GET /events
# GET /events.json
def index
@events = Event.all
end
# GET /events/1
# GET /events/1.json
def show
@event = Event.find(params[:id])
end
# GET /events/new
def new
@event = Event.new
end
# GET /events/1/edit
def edit
end
# POST /events
# POST /events.json
def create
@event = Event.new(event_params)
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
respond_to do |format|
if @event.update(event_params)
format.html { redirect_to @event, notice: 'Event was successfully updated.' }
format.json { render :show, status: :ok, location: @event }
else
format.html { render :edit }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
@event.destroy
respond_to do |format|
format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def event_params
params.require(:event).permit(:title, :date, :address, :cost, :image, :description)
end
end
活动模型:
class Event < ActiveRecord::Base
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
&#39;显示&#39;视图:
<p id="notice"><%= notice %></p>
<%= image_tag @event.image.url %>
<p>
<strong>Title:</strong>
<%= @event.title %>
</p>
<p>
<strong>Date:</strong>
<%= @event.date %>
</p>
<p>
<strong>Address:</strong>
<%= @event.address %>
</p>
<p>
<strong>Cost:</strong>
<%= @event.cost %>
</p>
<p>
<strong>Image:</strong>
<%= image_tag @event.image.url %>
</p>
<p>
<strong>Description:</strong>
<%= @event.description %>
</p>
<%= link_to 'Edit', edit_event_path(@event) %> |
<%= link_to 'Back', events_path %>
我真的很感谢你的帮助,因为我已经坚持了几天这个问题了。