【发布时间】:2014-01-10 20:18:40
【问题描述】:
路线
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
点击保存时出现错误信息。
ActionController::RoutingError:
No route matches [POST] "/locations/new"
查看
<%= simple_form_for :locations do |form| %>
<%= form.input :name %>
<%= form.input :street %>
<%= form.input :city %>
<%= form.input :state %>
<%= form.submit 'Create location' %>
<% end %>
使用 capybara 进行测试,当我点击保存时,它会创建一个新位置。我不太确定为什么它不知道发布路线是什么,因为我有新的和创建的路线。如果我将 binding.pry 放在 create 方法的正下方,它就不会被调用。所以我的 create 方法由于某种原因没有被调用。
编辑:
耙子路线
locations POST /locations(.:format) locations#
new_location GET /locations/new(.:format) locations#new
【问题讨论】:
标签: ruby-on-rails