Home>

ActiveRecord :: NotNullViolation in DrinksController # create
Mysql2 :: Error: Field'tag_name' doesn't have a default value

I'm getting an error and want to save the post and tag together

drinks_controller
def new
    @drink = DrinkTag.new
  end
  def create
    @drink = DrinkTag.new (drink_params)
    if @ drink.save
      redirect_to drinks_path
    else else
      render'new'
    end
  end
  def destroy
    Drink.find (params [: id]). destroy
    redirect_to root_path
  end

  private
  def drink_params
    params.require (: tweets_tag) .permit (: name ,: price ,: explain ,: image ,: tag_name) .merge (user_id: current_user.id)
  end
end
drinks/new.html.erb
     Enter product information   <% = form_with model: @drink, url: drinks_path, local: true do | f |%>
  <% = render'shared/error_messages', object: f.object%>        <% = f.label: tag_name%>    <% = f.text_field: tag_name%>                    Exhibition image
      <span>Required</span>                <p>        Click to upload file
      </p>    <% = f.file_field: image, id: "item-image"%>                          Product name
      <span>Required</span>        <% = f.text_area: name, class: "items-text", id: "item-name", placeholder: "Product name (up to 40 characters required)", maxlength: "40"%>            Product description
        <span>Required</span>            <% = f.text_area: explain, class: "items-text", id: "item-info", placeholder: "Coffee impression (up to 1,000 characters required)", rows: "7", maxlength: "1000" %>                        <span>price</span>    <span>Required</span>          <span>\</span>  <% = f.text_field: price, class: "price-input", id: "item-price", placeholder: "Example) 1000"%>  

        <% = f.submit "submit", class: "sell-btn"%>  <% = link_to'return', root_path, class: "back-btn"%>    <% end%>
drink_tag.rb
class DrinkTag
  include ActiveModel :: Model
  attr_accessor: name ,: price ,: explain ,: user_id ,: tag_name
  with_options presence: true do
    validates: explain
    validates: price
    validates: name
    validates: tag_name
    # tag and drink have a column name of name
    #Both are presense: true for now
    #You can post without tagging
    What to do if you need the name of #drink
    #drink [: name], only for drink name
    # Is it possible to apply validation?
  end
  def save
    drink = Drink.create (name: name, price: price, explain: explain, user_id: user_id)
    tag = Tag.where (tag_name: tag_name) .first_or_initialize
    # .first_or_initialize is the condition searched by where
    #If there is a record, return an instance of that record,
    # If not, it is a method to create a new instance.
    In # tag.rb, tag_name is used to restrict uniqueness.
    # I will play it when I wear it. First_or_initialize
    # Is necessary
    tag.save
    DrinkTagRelation.create (drink_id: drink.id, tag_id: tag.id)
  end
end
drink.rb
class Drink</pre>
<strong>tag.rb</strong>
<pre><code data-language = "Ruby">class Tag</pre>
<strong>drink_tag_relation.rb</strong>
<pre><code data-language = "Ruby">class DrinkTagRelation</pre>
<strong>What I tried</strong>
<p>I think that drink_tag.rb using formObject seems to be suspicious personally.<br />
I compared it with the teaching materials, but I don't think there is any difference.</p>
<p>Parameters too</p>
<pre><code data-language = "Ruby">{"authenticity_token" =>"T8/aOv7fmGDk6UO/GHiYkODYvpMTH/3tFP6sCW0QPVVaAVh6ZHcAd2xaTQzcuioVXdOOWYAFNHO8u3S0OmnG6Q =="
 "drink_tag" =>{"tag_name" =>"sourness",
   "image" =>  # >,
   "name" =>"Ethiopia",
   "explain" =>"Sour and ice cream is also recommended !!",
   "price" =>"1000"},


 "commit" =>"post"}


And tag_name, so it seems that there is a mistake in the controller or model, especially in drink_tag.rb, but I do not know now

  19: def create
    20: @drink = DrinkTag.new (drink_params)
 =>21: binding.pry
    22: if @ drink.save
    23: redirect_to drinks_path
    24: else
    25: render'new'
    26: end
    27: end
[1] pry (#<DrinksController>)>@drink
=>#<DrinkTag: 0x00007fad78565888 @ user_id = 6>[2] pry (#<DrinksController>)>params
=>"t9iR1cqdYFujJhDpgSr2eg7svBtkNwF7rrO4rwOdUaKiFhOVUDX4TCuVHlpF6ET/s + eM0fctyOUG9mASVOSqHg =" = ">" "", "price" =>""},

 "commit" =>"Post", "controller" =>"drinks", "action" =>"create"} permitted: false>

Since the content entered is not reflected in @drink, I still feel that the model is defective.

  • Answer # 1

    The parameter is

    "drink_tag" =>
      {"tag_name" =>"sourness",


    Strong parameters are flying even though it is flying

    params.require (: tweets_tag) .permit (...,: tag_name)

    It's because it's

    def create
        p drink_params
        @drink = DrinkTag.new (drink_params)

    I think that the parameters have disappeared when I look at it with something like

    @ drink.tag_name field becomes NULL
    Is it an error due to the NOT NULL constraint of DB?

    params.require (: drink_tag) .permit (...,: tag_name)


    I think it will be cured if you change it to