nested routes and form_for and models using has_one and belongs_to

纵饮孤独 提交于 2019-11-30 22:09:16

Does your profiles table has a user_id attribute?

In your routes, profile should be singular, since a user has one profile:

resources :users do
    resource  :profile
    resources :progress_charts
    resources :calories_journals
end

The route to a user profile will be users/:user_id/profile (and not users/:user_id/profile/:id

In your profiles_controller:

@profile = current_user.build_profile(params[:id]) # why params[:id]?
#it should just be
@profile = current_user.build_profile()
@user = User.find(params[:user_id])

And the form would be something like:

form_for [@user, @profile] do |f|...
end

Do you really want the user to create his profile like that? Usually, you would create the profile when a user register.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!