Reputation: 383
Apologies for the question, I'm still learning rails. Trying to update a specific user's home cities through a drop down. I followed all the steps as outlined in this question How to have a drop down <select> field in a rails form? and I'm not getting any errors. However, when I go to my rails console and do User.first - the homecities_id is still set as nil. Thank you guys so much for your help.
edit.html.erb
<div class="field">
<%= f.label :homecity %><br>
<%= f.collection_select :homecities_id, Homecity.all, :id, :Hometown %>
</div>
User.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, :omniauthable, omniauth_providers: [:facebook]
cattr_accessor :current_user
belongs_to :homecity, optional: true
end
Homecity.rb
class Homecity < ApplicationRecord
has_many :users
end
Application_controller.rb
class ApplicationController < ActionController::Base
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :name, :avatar, :homecities])
devise_parameter_sanitizer.permit(:account_update, keys: [:username,:name, :avatar, :homecities])
end
end
Migrations
class CreateHomecities < ActiveRecord::Migration[5.0]
def change
create_table :homecities do |t|
t.string :Hometown
t.timestamps
end
end
end
class AddHomecitiesRefToUsers < ActiveRecord::Migration[5.0]
def change
add_reference :users, :homecities, foreign_key: true
end
end
seed.rb
Homecity.destroy_all
bigapple = Homecity.create!(Hometown:"New York City")
techhub = Homecity.create!(Hometown:"San Francisco")
longhorns = Homecity.create!(Hometown:"Austin")
angels = Homecity.create!(Hometown:"Los Angeles")
windycity = Homecity.create!(Hometown:"Chicago")
hcards = Homecity.create!(Hometown:"Washington DC")
amazon = Homecity.create!(Hometown:"Seattle")
Upvotes: 0
Views: 474
Reputation: 36860
Since it's a belongs_to
it's only one homecity
and it's actually the homecity_id you're trying to pass to devise.
So instead of...
class ApplicationController < ActionController::Base
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :name, :avatar, :homecities])
devise_parameter_sanitizer.permit(:account_update, keys: [:username,:name, :avatar, :homecities])
end
end
You should do...
class ApplicationController < ActionController::Base
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :name, :avatar, :homecity_id])
devise_parameter_sanitizer.permit(:account_update, keys: [:username,:name, :avatar, :homecity_id])
end
end
And you'll need to modify your edit view to the singular...
<%= f.collection_select :homecity_id, Homecity.all, :id, :Hometown %>
Finally, do rake db:rollback
and change homecities
to homecity
in your migration and then do rake db:migrate
again.
Upvotes: 2
Reputation: 683
add_reference :users, :homecity, foreign_key: true
In the migration, only the first argument should be plural. Take a look here.
That is why you have homecitied_id
instead of homecity_id
in your database, which is not correct.
Also in the view you need to change to homecity_id
.
<%= f.collection_select :homecity_id, Homecity.all, :id, :Hometown %>
Upvotes: 2