rails routing问题编辑帖子

时间:2014-06-28 13:55:35

标签: ruby-on-rails rails-routing

我无法为其中一个帖子找到正确的edit_guide_path。我会假设'edit_guide_path(guide)'可以工作,但它没有。我使用友好ID,我认为这可能是问题的根源。我收到了错误

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

指南控制器

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

指南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

1 个答案:

答案 0 :(得分:1)

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

发出POST请求,但编辑操作需要GET请求。

你可以这样做

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

但这真的不是它应该如何运作的。使用link_to是首选方法。如果你需要它看起来像一个按钮,应用一些css爱它。

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