Home>
I'm making an event posting app, but it's not saved in the database after implementing the tag function.
Reference codeEvent 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.
PostscriptEvent 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
-
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 extraI'm rescued so I don't understand the error.
Exception measures Please remove once and try
Related articles
- ruby on rails data is not saved in db? ??
- ruby - i want to display the url saved in the db as a hyperlink in the view
- javascript - tab contents are not hidden
- ruby on rails - data cannot be saved in the intermediate table (parameters cannot be passed)
- ruby - account id cannot be saved with stripe
- ruby - validation does not start
- ruby on rails - date_select cannot be saved
- ruby on rails 6 - user information is not updated
- ruby - logut function does not work
- ruby - [rails] data is not saved due to activerecord :: notnullviolation error
- ruby - scss is not reflected
- ruby on rails - saved description in nested controller
- ruby - [rails] the created_at time saved in the db does not reach japan time
- ruby - [rails] actioncontroller::parametermissing is displayed and data is not saved
- ruby on rails - devise::sessionscontroller not found
- ruby on rails 6 - class does not end with end
- ruby - i want to solve the problem that posts are not saved
- ruby on rails - the image saved in the database cannot be displayed
- images are not displayed with css
Related questions
- ruby - passing variables using render partial
- ruby - deployment error on heroku
- ruby - output the total integer value held by each user
- ruby - i want to use the search method for active hash data
- ruby - destroy action does not respond
- ruby - how do you see the rails controller relative path? (carrierwave directory deleted)
- ruby - when deploying on capistrano, i get the error "access denied for user'ec2-user' @'localhost' (using password: no)&qu
- javascript - run multiple times with js + each do
- how to reflect the layout downloaded from the external site in ruby on rails?
- ruby - how nice! function routes do not pass well (relation) [rails]
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