简体   繁体   中英

Rails 5 – assign model attribute and save

I've got a problem with assigning some value to model attribute and save it. I've tried a lot of ways, but none worked.

@rating = Rating.new(rating_params)
@rating.save
@rating.update_attribute(:ip_address, request.remote_ip)

or

@rating = Rating.new(rating_params)
@rating.ip_address = request.remote_ip
@rating.save

Nothing is working for me :-( Everytime I got NULL in my database for column ip_address

Solution to debug in this situation.

@rating = Rating.new(rating_params)
@rating.ip_address = request.remote_ip

#check is't valid to save: true || false
@rating.valid?

# if false, print error messages
p @rating.errors
p @rating.errors.full_messages

#either way you can try save without validation.
@rating.save(:validate => false)

Also, What data type of column ip_address . You can't save integer into string or vice versa.

remote_ip by default string class, if want to save as integer.

Convert String Ip-address to Integer

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