2014-01-10 84 views
0

路线的ActionController :: RoutingError:没有路由匹配[POST] “/位置/新”

resources :locations, :only => [:new, :create]

控制器

class LocationsController < ApplicationController 
    def new 
    @location = Location.new 
    end 

    def create 
    @location = Location.new(location_params) 

    if @location.save 
     flash[:notice] = 'Created location successfully' 
     redirect_to new_location_path 
    else 
     flash[:notice] = 'Invalid information. Please try again' 
     render :new 
    end 
    end 

    private 

    def location_params 
    params.require(:location).permit(:name, :street, :city, :state) 
    end 
end 

错误消息,当我点击保存。

视图

<%= simple_form_for :locations do |form| %> 
    <%= form.input :name %> 
    <%= form.input :street %> 
    <%= form.input :city %> 
    <%= form.input :state %> 

    <%= form.submit 'Create location' %> 
<% end %> 

使用水豚测试,当我点击保存它创建了一个新的位置。我不太确定为什么它不知道邮政路线是什么,因为我有新的和创建的路线。如果我将一个binding.pry放在create方法的下面,它不会被调用。所以我的创建方法没有被某些原因调用。

编辑:

耙路线

locations POST /locations(.:format)      locations# 
new_location GET /locations/new(.:format)     locations#new 

回答

0

使用resources,该new动作要获取与GET而不是POSTcreate将是POST。检查您的rake routes

1

问题出在意见。

<%= simple_form_for :locations do |form| %> 
    <%= form.input :name %> 
    <%= form.input :street %> 
    <%= form.input :city %> 
    <%= form.input :state %> 

    <%= form.submit 'Create location' %> 
<% end %> 

我在调用位置符号时应该从控制器调用实例变量@location。

实际的问题是,轨道3.2.12不带私人PARAMS

+0

似乎是其中的form_for和等的符号使用在网络上大量的示例代码。 – Lori

相关问题