路由错误显示没有路由匹配[POST]“/ admin / locations / 1”

时间:2013-03-21 05:51:55

标签: ruby-on-rails ruby-on-rails-3

当我编辑我的位置时,我是ROR的新手,它会给我以下错误 No route matches [POST] "/admin/locations/1"

这里我使用的是rails 3.2.12

这是我的位置控制器

class Admin::LocationsController < ApplicationController


def index
    @location= Location.order("location desc")
end

def new
    @location=Location.new
end

def create
    @location = Location.new(params[:location])
    if @location.save
     # flash[:notice] = 'Location is successfully added in to list.'
      redirect_to :action => 'index'
    else
      render :action => 'new'
    end
end

def edit
   @location = Location.find(params[:id])
end

def update
    @location = Location.find(params[:id])
    if @location.update_attributes(params[:location])
      #flash[:notice] = 'Category is successfully updated.'
      redirect_to :action => 'index'
    else
      render :action => 'index'
    end
end

end

这是我的edit.html.erb

<h2>Edit Location</h2>
<%= simple_form_for(:location, :url => {:action => 'update', :id => @location.id}) do |f| %>
    <%= render(:partial => 'form', :locals => {:f => f}) %>  
    <%= submit_tag("Update",) %> <%= link_to("cancle", {:action => 'index'} )%>

<%end%>

这是我的route.rb

GuestHouse::Application.routes.draw do
  devise_for :customers

  namespace :admin do
    resources :locations
  end

并在我的index.html.erb中     &lt;%= link_to(“编辑”,{:action =&gt;'edit',:id =&gt; location.id},:class =&gt;'btn btn-info')%&gt;

3 个答案:

答案 0 :(得分:0)

对于修改表单,您可能希望使用PUT方法而不是POST。看起来你正在使用[SimpleForm] [1],它通常会为你处理给定模型的路径。您是否有任何理由未在Location的电话中传递simple_form_for个实例?我期待以下内容:

<%= simple_form_for @location do |f| %>
...

答案 1 :(得分:0)

<%= simple_form_for(:location, 
  :url => {:action => 'update', :id => @location.id},
  :method => 'put' ) do |f| %>

simple_form_for

中的传递方法

答案 2 :(得分:0)

这里是管理员/位置的路线,如下所示。

      admin_locations GET    /admin/locations(.:format)            admin/locations#index
                      POST   /admin/locations(.:format)            admin/locations#create
   new_admin_location GET    /admin/locations/new(.:format)        admin/locations#new
  edit_admin_location GET    /admin/locations/:id/edit(.:format)   admin/locations#edit
       admin_location GET    /admin/locations/:id(.:format)        admin/locations#show
                      PUT    /admin/locations/:id(.:format)        admin/locations#update
                      DELETE /admin/locations/:id(.:format)        admin/locations#destroy

因此,如果您要将表单发送到'更新'操作,则应提及如下路径。

<%= simple_form_for @location, :url => admin_location_path(@location),:html => { :method => "post"} do |f| %>