I am working on a site and ran across a problem where clicking on my 'delete' link brings up an error page. Works fine on localhost - can't figure out why this would be different in the Heroku site.
Does anyone have any idea how this could be?
My 'Pictures' controller:
class PicturesController < ApplicationController
def index
@pictures = Picture.all
end
def new
@pictures = Picture.all
@picture = Picture.new
end
def create
@picture = Picture.new(picture_params)
if @picture.save
redirect_to new_picture_path, notice: "You just uploaded a picture!"
else
render "new"
end
end
def destroy
Picture.find(params[:id]).destroy
redirect_to new_picture_path, notice: "You just deleted a picture!"
end
...
end
My picture/new view:
<h1>Upload a new picture</h1>
<br>
<div class="well">
<%= form_for @picture, :html => {:multipart => true} do |f| %>
<%= f.file_field :attachment %>
<%= f.submit "Upload", class: "btn btn-default" %>
<% end %>
<h1>Edit existing pictures </h1>
<% @pictures.each do |picture| %>
<%= image_tag picture.attachment_url(:thumb) %>
<%= link_to "Delete", picture_path(picture), method: :delete if user_signed_in? %>
<% end %>
Here are the heroku logs from when I try to delete a picture (starting with navigating to new picture page (new and edit functions in same view):
2015-11-15T16:13:49.189857+00:00 heroku[router]: at=info method=GET path="/pictures/new" host=designstatements.herokuapp.com request_id=ccb67000-b99f-4579-a664-097247d97414 fwd="73.238.59.224" dyno=web.1 connect=1ms service=46ms status=200 bytes=9770
2015-11-15T16:13:49.396294+00:00 heroku[router]: at=info method=GET path="/pictures/animate.min.css" host=designstatements.herokuapp.com request_id=fb87e1bd-b5cc-4f9f-9934-6c4076beff41 fwd="73.238.59.224" dyno=web.1 connect=1ms service=6ms status=404 bytes=1829
2015-11-15T16:13:49.478789+00:00 heroku[router]: at=info method=GET path="/pictures/js/bootstrap.min.js" host=designstatements.herokuapp.com request_id=2b36f60f-0e9a-498f-b210-74350c910540 fwd="73.238.59.224" dyno=web.1 connect=1ms service=8ms status=404 bytes=1829
2015-11-15T16:13:53.652050+00:00 heroku[router]: at=info method=GET path="/pictures/40" host=designstatements.herokuapp.com request_id=5dbb3c14-fab5-4525-a682-2d037b78dd4a fwd="73.238.59.224" dyno=web.1 connect=1ms service=10ms status=500 bytes=1754
As you can see, your assets could not be found (your log states a 404 error for /pictures/js/bootstrap.min.js
.
Rails uses jquery-ujs
, a Javascript library, to simulate DELETE
request, since browsers can only do GET
and POST
. jquery-ujs
turns normal links where you have set method: :delete
into POST
requests which have the parameter _method
set to delete
. This tells Rails to treat this request as a DELETE
-request.
Now, if your assets can't be loaded, neither will jquery-ujs
be loaded. With jquery-ujs
missing, your links can not be modified and the browser sends a GET-request instead of a fake DELETE
request.
To fix this, try setting
config.assets.compile = false
in your config/environments/production.rb
. This makes Heroku serve your assets. Maybe you have to change other settings as well (especially your asset path). But as soon as your JS and CSS files are loaded, the DELETE
links should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.