My Profiles controller:
def change_cover
@cover_name = params[:cover]
Profile.cover_name(@cover_name, session[:username])
end
My Model:
def self.cover_name(is, username)
Profile.new(:cover_name => is, :username => username).save
end
But it prints the next error:
ActiveRecord::UnknownAttributeError (unknown attribute: cover_name):
app/models/profile.rb:7:in `cover_name'
app/controllers/profiles_controller.rb:22:in `change_cover'
So, how can I insert it?
This error generates because your database table assuming your module has default settings, profiles
doesnot have a column called cover_name
You can do two things to test this
1 ) Check your DB directly. Example code in mysql
`desc profiles`
2 ) Login to your rails console and type
`Profile.new`
You are trying to create Profile with attribute profile_name and since profile has no attribute like profile_name it throws this error,
I guess you wanted to something like
def self.cover_name(covername, username)
ProfileCover.create(cover_name: covername, username: username)
end
Also, I see no association in your schema.rb, but I guess the profile cover should somehow be associated to your profile so you should do a migration creating one
rails g migration AddProfileToProfileCovers profile:belongs_to
In profile_cover.rb do
belongs_to :profile
In profile.rb
has_one :provile_cover
and update your method
def cover_name(covername, username)
self.profile_cover.create(cover_name: covername, username: username)
end
and call it with @profile.cover_name(covername, username)
or the other way around (not recommended)
def self.cover_name(covername, username, profile)
profile.profile_cover.create(cover_name: covername, username: username)
end
and call it with Profile.cover_name(covername, username, profile)
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.