Ruby on rails Can';t在Rails 5应用程序中通过表单创建新记录

Ruby on rails Can';t在Rails 5应用程序中通过表单创建新记录,ruby-on-rails,forms,postgresql,Ruby On Rails,Forms,Postgresql,这个表单工作得很好,但我有一段时间没有使用它了,我肯定我在某处引入了一个错误,但我无法跟踪它可能是什么。我还有另外两个类似的表单,它们按照预期提交给同一个postgres db。我可以编辑现有的记录,所以看起来这只是我遇到问题的new/create方法。日志实际上没有显示任何错误,它只是重定向回新视图: Processing by CoffeeshopsController#new as HTML Rendering coffeeshops/new.html.erb within layou

这个表单工作得很好,但我有一段时间没有使用它了,我肯定我在某处引入了一个错误,但我无法跟踪它可能是什么。我还有另外两个类似的表单,它们按照预期提交给同一个postgres db。我可以编辑现有的记录,所以看起来这只是我遇到问题的new/create方法。日志实际上没有显示任何错误,它只是重定向回新视图:

Processing by CoffeeshopsController#new as HTML
  Rendering coffeeshops/new.html.erb within layouts/application
  Rendered users/shared/_links.html.erb (1.4ms) [cache miss]
  Rendered partials/_mainnav.html.erb (10.2ms) [cache miss]
  Rendered partials/_footer.html.erb (1.1ms) [cache miss]
  Rendered coffeeshops/new.html.erb within layouts/application (22.4ms)
Completed 200 OK in 158ms (Views: 155.2ms | ActiveRecord: 0.0ms)

Started GET "/coffeeshops/new?utf8=%E2%9C%93&authenticity_token=2A91tyxSfricbX03rLRcx9Vqm%2FuWQiZSgwwjmmScH3GrTe63RRBMTHs72%2F4cQaoXD5yC8jxY2GaRLgHvdhgCbg%3D%3D&coffeeshop%5Bname%5D=New+Coffee+Shop&coffeeshop%5Bsnippet%5D=Great+new+coffee+on+the+banks+of+the+thames&coffeeshop%5Bdesc%5D=lovely+coffee+shop+serving+Red+Brick+coffee&coffeeshop%5Barea%5D=central&coffeeshop%5Burl%5D=website.com&coffeeshop%5Bemail%5D=&coffeeshop%5Baddress%5D=&coffeeshop%5Bpostcode%5D=&coffeeshop%5Blocale%5D=central&coffeeshop%5Bphone%5D=123&coffeeshop%5Bimage_thumb_path%5D=photo.jpg&coffeeshop%5Bimage_path%5D=photo.jpg&coffeeshop%5Bbeans%5D=Red+Brick&coffeeshop%5Blong_black%5D=2.5&coffeeshop%5Btag_list%5D=tag&commit=Save+Coffeeshop" for 127.0.0.1 at 2017-06-12 17:46:34 +0100
Processing by CoffeeshopsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2A91tyxSfricbX03rLRcx9Vqm/uWQiZSgwwjmmScH3GrTe63RRBMTHs72/4cQaoXD5yC8jxY2GaRLgHvdhgCbg==", "coffeeshop"=>{"name"=>"New Coffee Shop", "snippet"=>"Great new coffee on the banks of the thames", "desc"=>"lovely coffee shop serving Red Brick coffee", "area"=>"central", "url"=>"website.com", "email"=>"", "address"=>"", "postcode"=>"", "locale"=>"central", "phone"=>"123", "image_thumb_path"=>"photo.jpg", "image_path"=>"photo.jpg", "beans"=>"Red Brick", "long_black"=>"2.5", "tag_list"=>"tag"}, "commit"=>"Save Coffeeshop"}
  Rendering coffeeshops/new.html.erb within layouts/application
  Rendered users/shared/_links.html.erb (1.4ms) [cache miss]
  Rendered partials/_mainnav.html.erb (11.6ms) [cache miss]
  Rendered partials/_footer.html.erb (1.2ms) [cache miss]
  Rendered coffeeshops/new.html.erb within layouts/application (24.0ms)
Completed 200 OK in 162ms (Views: 159.9ms | ActiveRecord: 0.0ms)
咖啡店

更新
无法解决此问题,因此在一个新的部分中重新编写表单,该部分工作正常。

根据您的控制器,您的表单提交正常,但由于可能存在验证,因此无法保存记录。在此页面上显示错误消息将告诉您错误所在。我猜是DB级别的唯一约束。

根据您的日志,您的表单正在使用GET请求提交:

Started GET "/coffeeshops/new?utf8=%E2%9C%93&authenticity_token=2A91tyxSfricbX03rLRcx9Vqm%2FuWQiZSgwwjmmScH3GrTe63RRBMTHs72%2F4cQaoXD5yC8jxY2GaRLgHvdhgCbg%3D%3D&coffeeshop%5Bname%5D=New+Coffee+Shop&coffeeshop%5Bsnippet%5D=Great+new+coffee+on+the+banks+of+the+thames&coffeeshop%5Bdesc%5D=lovely+coffee+shop+serving+Red+Brick+coffee&coffeeshop%5Barea%5D=central&coffeeshop%5Burl%5D=website.com&coffeeshop%5Bemail%5D=&coffeeshop%5Baddress%5D=&coffeeshop%5Bpostcode%5D=&coffeeshop%5Blocale%5D=central&coffeeshop%5Bphone%5D=123&coffeeshop%5Bimage_thumb_path%5D=photo.jpg&coffeeshop%5Bimage_path%5D=photo.jpg&coffeeshop%5Bbeans%5D=Red+Brick&coffeeshop%5Blong_black%5D=2.5&coffeeshop%5Btag_list%5D=tag&commit=Save+Coffeeshop" for 127.0.0.1 at 2017-06-12 17:46:34 +0100
因此,Rails将请求路由到控制器中的
new
操作,如日志中的下一行所示:

Processing by CoffeeshopsController#new as HTML
问题的关键在于加载的是一个通用符号
:coffeeshop
,而不是在
新建
操作中实例化的对象

因为您只是使用了一个符号,而不是实际的对象,所以Rails不知道您想要提交POST请求

更改此选项:

<%= form_for :coffeeshop, url: coffeeshops_path do |f| %>

为此:

<%= form_for @coffeeshop do |f| %>


应该这样做。

请将您的视图代码添加到问题中。您构建表单的方式可能有问题。刚刚添加了上面的表单。您可以添加整个
咖啡店控制器吗?
?刚刚添加了参数,这可能是问题所在。我需要两个表单中没有的字段。我将移除并测试。不。这并没有解决问题。
Started GET "/coffeeshops/new?utf8=%E2%9C%93&authenticity_token=2A91tyxSfricbX03rLRcx9Vqm%2FuWQiZSgwwjmmScH3GrTe63RRBMTHs72%2F4cQaoXD5yC8jxY2GaRLgHvdhgCbg%3D%3D&coffeeshop%5Bname%5D=New+Coffee+Shop&coffeeshop%5Bsnippet%5D=Great+new+coffee+on+the+banks+of+the+thames&coffeeshop%5Bdesc%5D=lovely+coffee+shop+serving+Red+Brick+coffee&coffeeshop%5Barea%5D=central&coffeeshop%5Burl%5D=website.com&coffeeshop%5Bemail%5D=&coffeeshop%5Baddress%5D=&coffeeshop%5Bpostcode%5D=&coffeeshop%5Blocale%5D=central&coffeeshop%5Bphone%5D=123&coffeeshop%5Bimage_thumb_path%5D=photo.jpg&coffeeshop%5Bimage_path%5D=photo.jpg&coffeeshop%5Bbeans%5D=Red+Brick&coffeeshop%5Blong_black%5D=2.5&coffeeshop%5Btag_list%5D=tag&commit=Save+Coffeeshop" for 127.0.0.1 at 2017-06-12 17:46:34 +0100
Processing by CoffeeshopsController#new as HTML
<%= form_for :coffeeshop, url: coffeeshops_path do |f| %>
<%= form_for @coffeeshop do |f| %>