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.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 < 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 = @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
*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.
答案 0 :(得分:0)
看起来,根据您的控制器,您的表单提交正常但由于可能的验证而无法保存记录,在此页面上显示错误消息将告诉您错误的位置。我的猜测是数据库级别的唯一约束。
答案 1 :(得分:0)
根据您的日志
,您的表单是使用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| %>
应该这样做。