0

当我编辑我的位置时,我是 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 中作为 <%= link_to("Edit", {:action => 'edit', :id => location.id}, :class => 'btn btn-info')%>

4

3 回答 3

0

对于编辑表单,您可能希望使用该PUT方法而不是POST. 不过,您似乎正在使用 [SimpleForm][1],它通常会为您处理为给定模型构建路径。您是否有任何理由没有Location在您的调用中传递您的实例simple_form_for?我会期待类似以下的内容:

<%= simple_form_for @location do |f| %>
...
于 2013-03-21T06:00:45.227 回答
0
<%= simple_form_for(:location, 
  :url => {:action => 'update', :id => @location.id},
  :method => 'put' ) do |f| %>

传入方法simple_form_for

于 2013-03-21T06:03:42.670 回答
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
                      删除 /admin/locations/:id(.:format) admin/locations#destroy

因此,如果您想将表单发送到“更新”操作,您应该提及如下路径。

<%= simple_form_for @location, :url => admin_location_path(@location),:html => { :method => "post"} do |f| %>
于 2013-03-21T06:07:54.317 回答