Just started my first project in ROR and i'm facing a problem with build a relation between table.
As a noob i search a lot around the web, try plenty of stuff but can't figure out how to get this working.
Here's my issue: I built a User table for the login function - made with devise, now i want to build another table UserMeta to carry all the profile info of my users.
Here is the code of what i did:
app/models/user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :email, :encrypted_password
has_one :user_meta
end
*app/models/user_meta.rb*
class UserMeta < ActiveRecord::Base
attr_accessible :admin, :birthday, :company, :first_name, :job, :last_name, :phone, :rfid, :statut, :website, :user_id
belongs_to :users
end
class User
has_one :user_meta
end
*app/controllers/user_controllers.rb*
def new
#user = User.new
#meta = UserMeta.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #user }
end
end
app/views/users/new.html.erb
<%= form_for(#meta) do |f| %>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
It returns :
undefined method `meta_index_path' for #<#<Class:0x000001016f3f18>:0x00000100e9f6f0>
I know there is one or many mistake, what the best way to do this ? Some help will be appreciated :)
You can change to like this:
app/models/user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :encrypted_password
has_one :user_meta
accepts_nested_attributes_for :user_meta
end
app/models/user_meta.rb
class UserMeta < ActiveRecord::Base
attr_accessible :admin, :birthday, :company, :first_name, :job, :last_name, :phone, :rfid, :statut, :website, :user_id
belongs_to :user
end
app/controllers/user_controllers.rb
def new
#user = User.new
#user.build_meta_user
respond_to do |format|
format.html
format.json { render json: #user }
end
end
app/views/users/new.html.erb
<%= form_for #user, :url => {:action => 'create'} do |f| %>
<!-- User related fields -->
<%= f.fields_for :user_meta do |meta_f| %>
<div class="field">
<%= meta_f.label :phone %><br />
<%= meta_f.text_field :phone %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In Rails it's best to name your classes the same as your filenames. So app/models/user_meta.rb should be class UserMeta < ActiveRecord::Base.
There's no need to re-open the User-class in app/models/user_meta.rb. He'll know about app/models/user.rb already.
Change your belongs_to :users to belongs_to :user
In app/models/user.rb you can drop the 2nd attr_accessible :email.
Related
I'm looking to create a form with input fields for two models - Devise User model and Profile model. I want the profile model to be created with the fields and reference the User model when created.
Registration New view
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<!-- Devise Fields for User model -->
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
<%= f.password_field :password, autocomplete: "new-password" %>
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
<!-- Profile model -->
<%= f.fields_for :profile do |g| %>
<%= g.text_field :first_name %>
<%= g.text_field :last_name %>
<% end %>
<%= f.submit "Sign up" %>
<% end %>
User Model
has_one :profile, dependent: :destroy
attr_accessor :first_name, :last_name
accepts_nested_attributes_for :profile
Profile Model
belongs_to :user
Application Controller
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [ profile_attributes: [:first_name, :last_name]])
end
Devise Registration controller (override)
def new
super do |resource|
resource.build_profile
end
end
After filling out the form I receive an error message. Refer to the following image
Here is the code on Github https://github.com/Goeken/Speech-today
How should I go about this?
I had look into your github repository code & I found that your User model code is not correct. Here's the modified user model -
class User < ApplicationRecord
has_one :profile, dependent: :destroy
# You should not add first_name last_name validation message here,
# because those are profile model attributes, Not user model
#validates_presence_of :first_name, :last_name
accepts_nested_attributes_for :profile
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
And rest of code are OK.
I hope it will work.
I have the following models in my application:
User:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
before_destroy { roles.clear }
has_many :users_roles
has_many :roles, through: :users_roles
accepts_nested_attributes_for :roles
#accepts_nested_attributes_for :user_extras
#id :confirmable is activated
def confirmation_required?
false
end
end
Role:
class Role < ApplicationRecord
#before_destroy { users.clear }
has_many :users, through: :users_roles
end
and UserRoles:
class UsersRoles < ApplicationRecord
belongs_to :user
belongs_to :role
end
I also have a form that will accept nested attributes, which looks something like this:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<%= f.fields_for :roles do |field| %>
<%= field.label :name %></div>
<%= field.text_field :name %>
<% end %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
Also I have a controller that renders this form:
class Admin::AccountController < ApplicationController
before_action :authenticate_user!
layout "admin/layouts/dashboard"
def index
#resource = User.new
#resource2 = Role.new
#resource.roles << #resource2
p "==================="
p #resource
p #resource2
p "==================="
#resource.roles.build
# respond_to do |format|
# format.html
# format.json { render json: #resource }
# end
end
private
def admin_account_params
params.require(:resource).permit(:email, :password, :password_confirmation, roles_attributes: [ :name ])
end
end
The thing is when I visit the page, I get the following error:
uninitialized constant User::UsersRole
Extracted source (around line #9):
7
8
9
10
11
12
#resource = User.new
#resource2 = Role.new
#resource.roles << #resource2
p "==================="
I am not sure what is wrong with this at the moment. It will be great if you can spot the problem. Many thanks in advance.
Model classes by convention should be singular so the model class name should be...
class UsersRole < ApplicationRecord
belongs_to :user
belongs_to :role
end
This will automatically map to a database table named users_roles
If you insist on using a plural class name then you need to specify the class name explicitly
has_many :users_roles, class_name: 'UsersRoles'
And you'll need this in both the User model and the Role model
I use devise for user authentication. Have two models User and Profile which are associated to each other like this
#app/model/user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile, dependent: :destroy
enum role: [:admin, :seller, :buyer]
end
#app/model/profile.rb
class Profile < ApplicationRecord
belongs_to :user
end
As you can see I have also role for users.
Here is forms for registrations of both models.
#user registration form
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
...
<%= f.select :role, collection: User.roles.keys.last(2) %>
<%= f.input :mobile_number, required: true, autofocus: true %>
...
<% end %>
#profile registration form
<%= simple_form_for(#profile) do |f| %>
...
<% if current_user.seller? %>
<%= f.input :location, required: true %>
<% else %>
<%= f.input :company_code, required: true %>
<% end %>
...
<% end %>
Problem
When current user is seller I need to validate presence of :location , when user is buyer then validate presence of :company_code.
How can I achieve that? I appreciate any helps , Thanks!
validates method accepts if option:
# app/model/profile.rb
class Profile < ApplicationRecord
belongs_to :user
validates :location, presence: true, if: "user.seller?"
validates :company_code, presence: true, if: "user.buyer?"
end
Also, current_user.role == "seller" is obsolete. Rails automatically sets up additional methods in model for enum option values, therefore you may just write: current_user.seller?, etc.
I am unsure why my cocoon nested form is not appearing and any help would be much appreciated
the <button><%= link_to_add_association 'add a required skill', f, :requiredskills %></button> works perfectly well in displaying a form
but i am unsure why this is not rendering the form <%= render 'requiredskill_fields', :f => duty %>
what i would like to do is to have a form already displayed and when a user clicks add a required skill another form is displayed.
At present when a user clicks add a required skill that displays
another form - the main issue is having a form to be displayed
initially. I am assuming this <%= render 'requiredskill_fields', :f
=> duty %> is suppose to put a form in place but i am unsure why it is not working
user/_form.html.erb
<%= simple_form_for(#user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :firstname %>
<%= f.input :lastname %>
<h3>required skills & expertise</h3>
<div>
<div class="requiredskill_info" id="skill">
<%= f.simple_fields_for :requiredskills do |skill| %>
<%= render 'requiredskill_fields', :f => skill %>
<% end %>
<div class="add_requiredskill"><button><%= link_to_add_association 'add a required skill', f, :requiredskills %></button></div>
</div>
</div>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
user/_requiredskill_fields.html.erb
<div class="nested-fields">
<%= f.association :category_advert, collection: CategoryAdvert.all, prompt: "select a category" %>
<%= f.grouped_collection_select :category_advertskill_id, CategoryAdvert.order(:name), :category_advertskills, :name, :id, :name, {prompt: "Select a category"}, {class: "category_advertskill"} %>
<button><%= link_to_remove_association 'remove required skill', f %></button>
</div>
users_controller.rb
class UsersController < ApplicationController
respond_to :html, :xml, :json
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
#users = User.all
#user = current_user
end
def show
#resume = #user.resume
end
def edit
end
def update
#user.update(user_params)
redirect_to #user
end
private
def set_user
#user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:email, :firstname, :lastname, :city, :category_qualification_id, :category_careerlevel_id, :desiredjob, :category_distance_id, :preferedlocation, :category_notice_id, :category_country_id, :category_positiontype_id, :image, :cvattachment, :hidecv, :jobadvice, :validlicense, :owntransport, :considerrelocation, :preferredlocation, :neednotice, :stratdate, :availabletowork, :category_outsource_id, :category_advertskill_id, :category_age_id, languages_attributes: [:id, :name, :_destroy], requiredskills_attributes: [:id, :name, :category_advert_id, :category_advertskill_id, :category_year_id, :category_level_id, :_destroy])
end
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :requiredskills
has_many :category_advertskills, through: :requiredskills
has_many :category_adverts, through: :requiredskills
has_one :resume
accepts_nested_attributes_for :languages, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :requiredskills, :reject_if => :all_blank, :allow_destroy => true
end
def edit
#requiredskills = #user.requiredskills.present? ? #user.requiredskills : #user.requiredskills.build
end
change this code in controller
I have a very simple issue:
User model:
class User < ActiveRecord::Base
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :id, :email, :password, :password_confirmation, :remember_me,
:firstname, :lastname, :mobile_phone, :user_type, :department_id, :department_attributes
belongs_to :department
accepts_nested_attributes_for :department, :allow_destroy => false
Departments model:
class Department < ActiveRecord::Base
has_many :users
accepts_nested_attributes_for :users, :allow_destroy => true
I created a form to be able to select my departments member from my existing users using simple_form:
<%= simple_form_for #department, :validate => true do |form| %>
<%= form.error_messages %>
<%= form.association :users, :prompt => 'assign a user', :label => 'User'%>
<%= form.button :submit %>
<% end %>
Then I (try to) update my users via the department controller:
def update
#department = Department.find(params[:id])
respond_to do |format|
if #department.update_attributes(params[:department])
...
This generates following error:
WARNING: Can't mass-assign protected attributes: user_ids
My guess is that some devise settings generate this error but I don't know which ones.
Can you help? Thanks!
Add attr_accessible :user_ids to your Department model.