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

编程入门 行业动态 更新时间:2024-10-27 14:18:51
本文介绍了无法在Rails 5应用程序中通过表单创建新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

通过CoffeeshopsController处理#new as HTML 渲染布局/应用程序中的coffeeshops / new.html.erb 渲染用户/共享/ _links.html.erb(1.4ms)[高速缓存缺失] 渲染的部分/ _mainnav.html.erb (10.2ms)[高速缓存未命中] 渲染的部分/_footer.html.erb(1.1ms)[缓存缺失] 布局/应用程序中渲染的coffeeshops / new.html.erb(22.4ms)在158毫秒内完成200 OK(查看:155.2ms | ActiveRecord:0.0ms) 开始GET/咖啡店/新?utf8 =%E2%9C%93& authenticity_token = 2A91tyxSfricbX03rLRcx9Vqm%2FuWQiZSgwwjmmScH3GrTe63RRBMTHs72%2F4cQaoXD5yC8jxY2GaRLgHvdhgCbg %3D%3D&放大器;咖啡店%5Bname%5D =新+咖啡+店和放大器;咖啡店%5Bsnippet%5D =大+上+中+银行+在+泰晤士与放大器的+新+咖啡+;咖啡店%5Bdesc%5D =可爱+咖啡+店铺+服务+红+ +砖咖啡&安培;咖啡店%5Barea%5D =中央&安培;咖啡店%5Burl%5D = website和C offeeshop%5Bemail%5D =安培;咖啡店%5Baddress%5D =安培;咖啡店%5Bpostcode%5D =安培;咖啡店%5Blocale%5D =中央&安培;咖啡店%5Bphone%5D = 123&安培;咖啡店%5Bimage_thumb_path%5D = photo.jpg&安培; 2017年的咖啡店%5Bimage_path%5D = photo.jpg& coffeeshop%5Bbeans%5D = Red + Brick& coffeeshop%5Blong_black%5D = 2.5& coffeeshop%5Btag_list%5D = tag& commit = Save + Coffeeshop 06-12 17:46:34 +0100 CoffeeshopsController处理#new as HTML 参数:{utf8=>✓,authenticity_token=>2A91tyxSfricbX03rLRcx9Vqm / uWQiZSgwwjmmScH3GrTe63RRBMTHs72 / 4cQaoXD5yC8jxY2GaRLgHvdhgCbg ==,coffeeshop=> {name=>New Coffee Shop,snippet=>泰晤士河畔的新咖啡,desc=>可爱的咖啡店铺服务红砖咖啡,区域=>中央,url=website,email=>,address=>,postcode =123,image_thumb_path=>photo.jpg,image_path=>photo.jpg, 豆 =Red Brick,long_black=>2.5,tag_list=>tag},commit=>Save Coffeeshop} 渲染coffeeshops / new.html .erb在布局/应用程序中渲染用户/共享/ _links.html.erb(1.4ms)[高速缓存缺失] 渲染的部分/_mainnav.html.erb(11.6ms)[高速缓存缺失] 渲染的partials / _footer.html.erb(1.2ms)[缓存缺失] 布局/应用程序中渲染的coffeeshops / new.html.erb(24.0ms)在162ms内完成200 OK(查看: | 159.9ms | ActiveRecord:0.0ms)

coffeeshop.rb

class Coffeeshop< ApplicationRecord paginates_per 5 包含PgSearch pg_search_scope:search_by_full_name,对:[:name] 需要'acts-as-taggable-on' acts_as_taggable #acts_as_taggable_on:tag_list $ b has_many:comments,as::commentable belongs_to:roaster belongs_to:user has_many:favorite_coffeeshops#只是'关系' has_many:favorited_by,through::favorite_coffeeshops,source::user $ b $ has_many:bookmarked_coffeeshops#只是'关系' has_many:bookmarked_by,通过:: bookmarked_coffeeshops,source :: user 验证:name,:snippet,:area,:image_thumb_path,:image_path,:presence =>真 $ b扩展FriendlyId friendly_id:名称,用途:[:slugged,:finders] 私人 def should_generate_new_friendly_id? slug.nil? || name_changed? end end

coffeeshop_controller.rb

class CoffeeshopsController< ApplicationController http_basic_authenticate_with名称:****,密码:****,除外:[: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 = @ commentablements @comment = Comment.new @locale_cafe = Coffeeshop.where(locale:@ coffeeshop.locale) @fave_count = @ coffeeshop.favorited_by @user = User.all @currentuser = current_user 结束 def新 @coffeeshop = Coffeeshop.new 结束 结束编辑 @coffeeshop = Coffeeshop.friendly .find(params [:id])结束 def创建 @coffeeshop = Coffeeshop.new(coffeeshop_params) if @ coffeeshop.save redirect_to @coffeeshop else 渲染'new ' end end def update @coffeeshop = Coffeeshop.find(params [:id]) if @coffeeshop。更新(coffeeshop_params) redirect_to @coffeeshop 其他渲染'编辑'结束结束 def收藏 @coffeeshop = Coffeeshop.find(params [:id]) type = params [:type] if type ==favorite current_user.favorites<< @coffeeshop redirect_to:返回,注意:您最喜欢的#{@coffeeshop.name} elsif type ==unfavorite current_user.favorites.delete(@coffeeshop ) redirect_to:返回,注意:不确定的#{@coffeeshop.name} else #类型丢失,没有任何反应 redirect_to:返回,注意: 什么都没发生。 end end def书签 @coffeeshop = Coffeeshop.find(params [:id]) type = params [:type] if type ==bookmarked current_user.bookmarks<< @coffeeshop redirect_to:返回,注意:您已将#{@coffeeshop.name添加到书签中 elsif type ==unbookmark current_user.bookmarks.delete(@coffeeshop ) redirect_to:注意:您删除了#{@coffeeshop.name}书签 其他#类型丢失,没有任何反应 redirect_to:返回,注意:什么都没发生。 end end private def coffeeshop_params params.require(:coffeeshop).permit(:name,:desc,:区域,:url,:email,:address,:postcode,:locale,:phone,:image_path,:image_thumb_path,:snippet,:beans,:long_black,:tag_list) end end

*表格**

<%= form_for:coffeeshop,url:coffeeshops_path do | f | %GT; < p> <%= f.label:Name%>< br> <%= f.text_field:name%> < / p> < p> <%= f.label:Snippet%>< br> <%= f.text_area:snippet%> < / p> < p> <%= f.label:Desciption%>< br> <%= f.text_area:desc%> < / p> < p> <%= f.label:Area%>< br> <%= f.text_area:area%> < / p> < p> <%= f.label:URL%>< br> <%= f.text_area:url%> < / p> < p> <%= f.label:电子邮件%>< br> <%= f.text_area:电子邮件%> < / p> < p> <%= f.label:地址%>< br> <%= f.text_area:address%> < / p> < p> <%= f.label:邮政编码%>< br> <%= f.text_area:postcode%> < / p> < p> <%= f.label:Locale%>< br> <%= f.text_area:locale%> < / p> < p> <%= f.label:手机%>< br> <%= f.text_area:phone%> < / p> < p> <%= f.label:缩略图%>< br> <%= f.text_area:image_thumb_path%> < / p> < p> <%= f.label:图片%>< br> <%= f.text_area:image_path%> < / p> < p> <%= f.label:Beans%>< br> <%= f.text_area:beans%> < / p> < p> <%= f.label:Price_of_long_black%>< br> <%= f.text_area:long_black%> < / p> < p> <%= f.label:tag_list,'标记(以逗号分隔)'%>< br /> <%= f.text_area:tag_list%> < p> < p> <%= f.submit%> < / p> <%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

更新 无法执行此操作,因此需要在新的部分中重新编写表单。根据您的日志,您的表单正在使用GET请求进行提交:

解决方案

pre> 开始GET/咖啡店/新?utf8 =%E2%9C%93&authenticity_token = 2A91tyxSfricbX03rLRcx9Vqm%2FuWQiZSgwwjmmScH3GrTe63RRBMTHs72%2F4cQaoXD5yC8jxY2GaRLgHvdhgCbg%3D%3D& coffeeshop%5Bname%5D = New + Coffee +店&放大器;咖啡店%5Bsnippet%5D =大+新+咖啡在+的+银行+ +中+泰晤士与放大器+;咖啡店%5Bdesc%5D =可爱+咖啡+店铺+服务+红+砖+咖啡和放大器;咖啡店%5Barea%5D =中央&安培;咖啡店%5Burl%5D = website&安培;咖啡店%5Bemail%5D =安培;咖啡店%5Baddress%5D =安培;咖啡店%5Bpostcode%5D =安培;咖啡店%5Blocale%5D =中央&安培;咖啡店%5Bphone%5D = 123&安培;咖啡店%5Bimage_thumb_path%5D = photo.jpg&安培;咖啡店%5Bimage_path%5D = photo.jpg&安培;咖啡店%5Bbeans%5D =红+砖瓦和安培;咖啡店%5Blong_black%5D = 2.5&安培;咖啡店%5Btag_list% 5D = tag& commit = Save + Coffeeshopfor 2017-03-01 17:46:34 +0100

正因为如此,Rails将请求路由到控制器中的 new 动作,因为日志中的下一行说:

由CoffeeshopsController处理#new as HTML

问题的关键在于你加载了一个通用符号:coffeeshop 而不是你在 new 中实例化的对象c $ c> action。

因为你只是使用一个符号而不是一个实际的对象,Rails并不知道您需要提交POST请求。

更改此项:

<%= form_for:coffeeshop,url:coffeeshops_path do | f | %GT; b

<%= form_for @coffeeshop do | f | %GT;

应该有所斩获。

This form was working perfectly but I've not used it for a little while and I'm sure I've introduced an error somewhere, but I can't track what it might be. I have two other similar forms that submit to the same postgres db as expected. I can edit an existing record ok, so looks like it is just the new/create method I'm having problems with. The log doesn't actually show any error, it just redirects back to the new view:

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&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", "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 < ApplicationRecord paginates_per 5 include PgSearch pg_search_scope :search_by_full_name, against: [:name] require 'acts-as-taggable-on' acts_as_taggable #acts_as_taggable_on :tag_list has_many :comments, as: :commentable belongs_to :roaster belongs_to :user has_many :favorite_coffeeshops# just the 'relationships' has_many :favorited_by, through: :favorite_coffeeshops, source: :user has_many :bookmarked_coffeeshops# just the 'relationships' has_many :bookmarked_by, through: :bookmarked_coffeeshops, source: :user validates :name, :snippet, :area, :image_thumb_path, :image_path, :presence => 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 = @commentablements @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

*the form**

<%= form_for :coffeeshop, url: coffeeshops_path do |f| %> <p> <%= f.label :Name %><br> <%= f.text_field :name %> </p> <p> <%= f.label :Snippet %><br> <%= f.text_area :snippet %> </p> <p> <%= f.label :Desciption %><br> <%= f.text_area :desc %> </p> <p> <%= f.label :Area %><br> <%= f.text_area :area %> </p> <p> <%= f.label :URL %><br> <%= f.text_area :url %> </p> <p> <%= f.label :email %><br> <%= f.text_area :email %> </p> <p> <%= f.label :Address %><br> <%= f.text_area :address %> </p> <p> <%= f.label :Postcode %><br> <%= f.text_area :postcode %> </p> <p> <%= f.label :Locale %><br> <%= f.text_area :locale %> </p> <p> <%= f.label :Phone %><br> <%= f.text_area :phone %> </p> <p> <%= f.label :Thumbnail %><br> <%= f.text_area :image_thumb_path %> </p> <p> <%= f.label :Image %><br> <%= f.text_area :image_path %> </p> <p> <%= f.label :Beans %><br> <%= f.text_area :beans %> </p> <p> <%= f.label :Price_of_long_black %><br> <%= f.text_area :long_black %> </p> <p> <%= f.label :tag_list, 'Tags (separated by commas)' %><br/> <%= f.text_area :tag_list %> <p> <p> <%= f.submit %> </p> <% 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

Update Couldn't work this out so re-wrote the form in a new partial which works.

解决方案

Your form is submitting using a GET request, according to your log:

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&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

Because of this, Rails is routing the request to the new action in the controller, as the next line in the log says:

Processing by CoffeeshopsController#new as HTML

The crux of the problem is that your loading a generic symbol :coffeeshop instead of the object you instantiated in the new action.

Because you're just using a symbol and not an actual object, Rails doesn't know you want to submit a POST request.

Changing this:

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

to this:

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

should do the trick.

更多推荐

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

本文发布于:2023-08-06 04:36:19,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   应用程序   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!