2017-06-12 72 views
0

此表單工作正常,但我一段時間沒有使用它,並且我確定我在某處引入了錯誤,但無法跟蹤它可能是什麼。我有兩個其他類似的表單提交到相同的postgres數據庫如預期。我可以編輯現有的記錄,因此看起來像是我遇到問題的新/創建方法。日誌實際上並不顯示任何錯誤,它只是重定向到新的觀點:無法通過Rails 5應用程序中的表單創建新記錄

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 

*表格**

<%= 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 

更新 不能工作了這一點,所以重新寫的形式,新的部分,其工作原理。

+0

請將您的視圖的代碼添加到問題中。這似乎是您構建表單的方式可能存在的問題。 – seancdavis

+0

剛剛添加上面的表格。 –

+0

你可以添加整個'coffeeshop_controller'嗎? –

回答

0

看起來,基於您的控制器,您的表單提交正常,但由於可能的驗證無法保存記錄,因此在此頁面上顯示錯誤消息會告訴您錯誤在哪裏。我的猜測是在數據庫層面的獨特限制。

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

應該做的伎倆。

相關問題