JapanRob JapanRob - 3 months ago 7
Ruby Question

Rails "No route matches POST /images/1" error

I looked through several other similar help questions, and I'm not seeing my error. I have destroy, new, create all working, but post and show throw errors. Also oddly enough, clicking the 'edit' button on the index view throws an error, but going directly to http://localhost:3000/images/1 gives me the 'show' page (no idea why it isn't giving an edit page).

relevant files:

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


Note: I have also added the lines

get 'images/edit'

get 'images/details'


But to no avail.

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>&nbsp;</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>

Answer

I suppose there are no HTTP methods like this:

method: :edit, method: :show

Try to do smth like this:

link_to "Edit", edit_image_path(image)
link_to "Show", image_path(image)

And remove this line from routes.rb

get 'images/edit'

resources has already created rout to edit action for you. You can see it http://localhost:3000/rails/info/routes