0

I have 2 models

  1. User
  2. Profile (belongs_to User)

I have 'username' in Profile model. I want to take value of 'username' in User new form and save it to username of Profile. How can I do that. Please suggest code.

Adt
  • 495
  • 6
  • 19
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. – honk Jan 22 '15 at 16:21
  • This is really an architectural question. Perhaps you could help us by explaining why `username` is not stored in `User`? – Stratus3D Jan 22 '15 at 16:25
  • 1
    I agree with @Stratus3D, still if this could help you , http://railscasts.com/episodes/196-nested-model-form-part-1, http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html – pkrawat1 Jan 22 '15 at 16:31
  • @Stratus3D I want to create friendly_id url for profile view but i can't access User.username due to some calling issues thats why – Adt Jan 22 '15 at 16:31
  • I am still unsure how your models are structured. Can you paste in some of the code from the models? – Stratus3D Jan 22 '15 at 16:38
  • @Stratus3D check this http://stackoverflow.com/questions/28051548/nomethoderror-at-undefined-method-name-for-nilnilclass?noredirect=1#comment44525527_28051548 – Adt Jan 22 '15 at 16:39

1 Answers1

1

Try this

Add these in your Gemfile

gem 'simple_form'
gem 'nested_form'

In your user model add this

accepts_nested_attributes_for :profile

In your form under simple_nested_form_for

= f.simple_fields_for :profile do |p|
  = p.input :username

In you user controller add this to permit profile parameters

def user_params
  params.require(:user).permit(
    #User Attribute Names,
    profile_attributes: [#Profile atrribute names]
  )
pkrawat1
  • 671
  • 7
  • 18
  • You answered exactly what i want but its giving error. Please see sample code and suggest : http://pastebin.com/9RLeQBMF – Adt Jan 22 '15 at 17:03
  • what is the error you are getting, can you show me that too. – pkrawat1 Jan 22 '15 at 17:14
  • I found the cause of error. Thanks for all your help. You really solved my problem. – Adt Jan 22 '15 at 17:16