无法通过Rails 5应用程序中的表单创建新记录

这个表格工作得很好,但是我已经有一段时间没用过它了,我确定我在某个地方引入了一个错误,但我无法追踪它可能是什么。 我有两个其他类似的表单,按预期提交到相同的postgres数据库。 我可以编辑现有的记录,所以看起来它只是我遇到问题的新/创建方法。 日志实际上没有显示任何错误,它只是重定向回新视图:

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) 

coffeeshop.rb

 class Coffeeshop  true extend FriendlyId friendly_id :name, use: [:slugged, :finders] private def should_generate_new_friendly_id? slug.nil? || name_changed? end end 

coffeeshop_controller.rb

 class CoffeeshopsController < ApplicationController http_basic_authenticate_with name: "****", password: "****", except: [:index, :show, :favorite, :bookmarked] def index if params[:tag] @coffeeshops = Coffeeshop.tagged_with(params[:tag]) else @coffeeshops = Coffeeshop.all end @coffeeshops = @coffeeshops.order("created_at ASC").page params[:page] end def show @coffeeshop = Coffeeshop.find(params[:id]) @last3_coffeeshops = Coffeeshop.last(3) @commentable = @coffeeshop @comments = @commentable.comments @comment = Comment.new @locale_cafe = Coffeeshop.where(locale: @coffeeshop.locale) @fave_count = @coffeeshop.favorited_by @user = User.all @currentuser = current_user end def new @coffeeshop = Coffeeshop.new end def edit @coffeeshop = Coffeeshop.friendly.find(params[:id]) end def create @coffeeshop = Coffeeshop.new(coffeeshop_params) if @coffeeshop.save redirect_to @coffeeshop else render 'new' end end def update @coffeeshop = Coffeeshop.find(params[:id]) if @coffeeshop.update(coffeeshop_params) redirect_to @coffeeshop else render 'edit' end end def favorite @coffeeshop = Coffeeshop.find(params[:id]) type = params[:type] if type == "favorite" current_user.favorites << @coffeeshop redirect_to :back, notice: "You favorited #{@coffeeshop.name}" elsif type == "unfavorite" current_user.favorites.delete(@coffeeshop) redirect_to :back, notice: "Unfavorited #{@coffeeshop.name}" else # Type missing, nothing happens redirect_to :back, notice: "Nothing happened." end end def bookmarked @coffeeshop = Coffeeshop.find(params[:id]) type = params[:type] if type == "bookmarked" current_user.bookmarks << @coffeeshop redirect_to :back, notice: "You bookmarked #{@coffeeshop.name}" elsif type == "unbookmark" current_user.bookmarks.delete(@coffeeshop) redirect_to :back, notice: "You removed #{@coffeeshop.name} bookmark" else # Type missing, nothing happens redirect_to :back, notice: "Nothing happened." end end private def coffeeshop_params params.require(:coffeeshop).permit(:name, :desc, :area, :url, :email, :address, :postcode, :locale, :phone, :image_path, :image_thumb_path, :snippet, :beans, :long_black, :tag_list) end end 

*表格**

  
















coffeeshop_params

  private def coffeeshop_params params.require(:coffeeshop).permit(:name, :desc, :area, :url, :email, :address, :postcode, :locale, :phone, :image_path, :image_thumb_path, :snippet, :beans, :long_black, :tag_list, :image2, :image3) end 

更新无法解决这个问题所以重新编写了一个新的部分表单。

看起来,基于您的控制器,您的表单提交正常但由于可能的validation而无法保存记录,在此页面上显示错误消息将告诉您错误的位置。 我的猜测是数据库级别的唯一约束。

根据您的日志,您的表单是使用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而不是您在new操作中实例化的对象。

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

改变这个:

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

对此:

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

应该做的伎俩。