rico_mac
rico_mac

Reputation: 888

rails routing issue to edit post

I am having trouble finding the correct edit_guide_path for one of my posts. I would have assumed that 'edit_guide_path(guide)' would work, but it doesnt. I am using friendly id and I think that might be the source of the problem. I get the error

'No route matches [POST] "/guides/david-knight-maurizio-miele-anita-rowell-a-triangle-of-modern-art/edit"'

Guide controller

class GuidesController < ApplicationController
before_action :authenticate_admin!, only: [:new, :create, :destroy, :edit]

def show
 @guide = Guide.friendly.find params[:id]
 render :layout => 'guide_show'
end

def index
 @title = "What's on?"
 @all_guides = Guide.all  
 @guide = Guide.up_coming
 @finished = Guide.finished.last(5)
 @current = Guide.active_at_date.all
end

def new
  @guide = Guide.new
end

def edit 
 @guide = Guide.friendly.find params[:id]
end

def update
 @guide = Guide.friendly.find params[:id]

 if @guide.update(guide_params) 
   flash[:notice] = "You have succesfully editted #{@guide.title}"
  redirect_to '/'
 else
  render '/new'
 end
end

def create
@guide = Guide.new(guide_params)
if @guide.save
  redirect_to '/guides'
else
    render 'new'
end
  end

 def destroy
  @guide = Guide.find params[:id]
  flash[:notice] = "You have succesfully deleted #{@guide.title}"
  @guide.destroy
  redirect_to '/guides'
 end

private

def guide_params
  params.require(:guide).permit(:id, :title, :description, :image, :image_extra, :date_starting, :date_ending, :extra_info)
end

end

Guide index.html.erb

<div class='row guide-row'>
 <% @all_guides.in_groups_of(2, false).each do |guide_row| %>
   <% for guide in guide_row %>
    ...............
    <%= button_to "Delete", guide_path(guide), method: :delete, data: { confirm: 'Confirm' }, class: 'btn btn-default' %>
    <%= button_to "Edit", edit_guide_path(guide), class: 'btn btn-default' %>

rake routes

 guides GET    /guides(.:format)                 guides#index
                      POST   /guides(.:format)                 guides#create
            new_guide GET    /guides/new(.:format)             guides#new
           edit_guide GET    /guides/:id/edit(.:format)        guides#edit
                guide GET    /guides/:id(.:format)             guides#show
                      PATCH  /guides/:id(.:format)             guides#update
                      PUT    /guides/:id(.:format)             guides#update
                      DELETE /guides/:id(.:format)             guides#destroy

Upvotes: 0

Views: 40

Answers (1)

Eyeslandic
Eyeslandic

Reputation: 14890

<%= button_to "Edit", edit_guide_path(guide), class: 'btn btn-default' %>

Issues a POST request, but the edit action requires a GET request.

You can do it like this

<%= button_to "Edit", guide, class: 'btn btn-default', method: :get %>

but that is really not how it's supposed to work. Using a link_to is the preferred method. If you need it look like a button, apply some css love to it.

<%= link_to "Edit", guide, class: 'btn btn-default' %>

Upvotes: 1

Related Questions