简体   繁体   中英

Can't create new record via form in Rails 5 app

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.

It looks like, based on your controller, your form is submitting fine but it is unable to save the record because of possibly validations, displaying the error messages on this page will tell you where your error is. My guess is unique constraints at the DB level.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM