Home>

I'm making an event posting app, but it's not saved in the database after implementing the tag function.

Reference code

Event controller

def index
    @tag_list = Tag.all
    @events = Event.all
    @event = current_user.events.new
  end
  def new
    @event = Event.new
  end
   def create
     # binding.pry
     @event = Event.new (event_params)
     # binding.pry
     if @ event.save
     # binding.pry
       tag_list = tag_params [: tag_names] .delete (""). split (",")
       @ event.save_tags (tag_list)
       redirect_to root_path
     else else
       render'new'
     end
   end
Private
Def event_params
    params.require (: event) .permit (: name,: expectation,: facility_id,: scale_id,: category_id,: volunteer, images: []). merge (user_id: current_user.id)
  end
  def tag_params
    params.require (: event) .permit (: tag_names)
  end

Event model

class Event</pre>
<p>Tag model</p>
<pre><code>class Tag</pre>
<p><br />
New registration view</p>
<pre><code><% = form_with model: @event, url: events_path, local: true do | f |%>
          <% = render'shared/error_messages', model: f.object%>
            
          <label>event name</label>
          <% = f.text_field: name, class :: form_control%>
          
<Dib,>
      <label>tag</label>
      <% = f.text_field: tag_names, class: "input-tag"%>
      
        
      
What I went to

After checking the params in binding.pry after the save method

"zJzUCe4RFWoSTgLeXk64nRjg9Db7BuoC6ZBZkh/1RaRvqyCH5BHfdbB6iScb5n + JGEYk9vpEanaf/Kh5MwUFgQ ==", aaaa "=" "," volunteer "=>" "," tag_names "=>" s, aaaa, rrrrr "," facility_id "=>" 2 "," scale_id "=>" 3 "," category_id "=>" 2 "} permitted: false>, "commit" =>"save", "controller" =>"events", "action" =>"create"} permitted: false>
[2] pry (#<EventsController>)>@ event.save
=>true

I was able to confirm that the save method was working, but it was not reflected in the database. I don't know where the cause is, so I'd appreciate it if you could give me some advice.

Postscript

Event model

def save_tags (tag_list)
    26: # binding.pry
    27: tag_list.each do | tag |
    28: unless find_tag ​​= Tag.find_by (tag_name: tag.downcase)
 =>29: binding.pry
    30: # binding.pry
    31: # begin
    32: # self.tags.create! (Tag_name: tag)
    33:
    34: # rescue
    35: # nil
    36: # end
    37: # else
    38: #EventTagRelation.create! (event_id: self.id, tag_ids: find_tag.id)
    39: # binding.pry
    40: end
    41: end
    42: end

pry content

[1] pry (#<Event>)>tag_name
NameError: undefined local variable or method `tag_name'for #<Event: 0x00007fee549252d0>
from /Users/user/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activemodel-6.0.3.4/lib/active_model/attribute_methods.rb:432:in `method_missing'
[2] pry (#<Event>)>tag.downcase
=>"s"

I entered "s, aaaa, rrrrr" in the tag

  • Answer # 1

    The description of validation in the tag model was incorrect.
    validates: tagname, presence: true
    From
    validates: tag_name, presence: true
    I was able to implement it at

  • Answer # 2

    Is there an error?
    If it is out, please put it on. Otherwise, the cause cannot be grasped.

    Thinking in Esper mode
    validates: tagname, uniqueness: true
    This feels extra

    I'm rescued so I don't understand the error.
    Exception measures Please remove once and try