我查看了其他几个类似的帮助问题,我没有看到我的错误。我有破坏,新的,创建所有工作,但发布和显示抛出错误。同样奇怪的是,单击索引视图上的“编辑”按钮会引发错误,但直接转到http://localhost:3000/images/1会给我“显示”页面(不知道为什么它没有给出编辑页面)。
相关文件:
images_controller.rb
class ImagesController < ApplicationController
def index
@images = Image.all
end
def new
@image = Image.new
end
def create
@image = Image.new(image_params)
if @image.save
redirect_to images_path, notice: "Your image #{@image.name} has been uploaded."
else
render "new"
end
end
def edit
@image = Image.find(params[:id])
end
def destroy
@image = Image.find(params[:id])
@image.destroy
redirect_to images_path, notice: "The image #{@image.name} has been removed from the database."
end
def show
@image = Image.find(params[:id])
end
private
def image_params
params.require(:image).permit(:name, :attachment)
end
end
的routes.rb
Rails.application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
resources :images#, only: [:index, :new, :create, :update, :edit, :show, :destroy]
root "images#index"
end
注意:我还添加了行
get 'images/edit'
get 'images/details'
但无济于事。
index.html.erb:
<% if !flash[:notice].blank? %>
<div class="alert alert-info">
<%= flash[:notice] %>
</div>
<% end %>
<br />
<%= link_to "New Image", new_image_path, class: "btn btn-primary" %>
<br />
<br />
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>View Link</th>
<th> </th>
</tr>
</thead>
<tbody>
<% @images.each do |image| %>
<tr>
<td><%= image.name %></td>
<td><%= link_to "#{image.name}", image.attachment_url %></td>
<td><%= button_to "Edit", image, method: :edit, class: "btn btn-primary", confirm: "edit #{image.name}?" %></td>
<td><%= button_to "Show", image, method: :show, class: "btn btn-primary"%></td>
<td><%= button_to "Delete", image, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{image.name}?" %></td>
</tr>
<% end %>
</tbody>
</table>
答案 0 :(得分:2)
button_to会发布您需要link_to "edit"
发出请求的帖子请求。当您在浏览器中输入http://localhost:3000/images/1时,您正在获取请求,以便它可以正常工作。
link_to "Edit", edit_image_path image
检查这些链接是否为link_to和button_to http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
答案 1 :(得分:2)
我想没有像这样的HTTP方法:
method: :edit, method: :show
尝试像这样做:
link_to "Edit", edit_image_path(image)
link_to "Show", image_path(image)
并从routes.rb
中删除此行get 'images/edit'
资源已经创建了路径来为您编辑操作。你可以看到它http://localhost:3000/rails/info/routes