building a has_one relation with devise user in rails -
im new rails , having difficulty creating profile devise user
here profilecontroller:
class profilescontroller < applicationcontroller before_action :set_profile, only: [:show, :edit, :update, :destroy] before_action :authenticate_user! def index @profiles = profile.all end def new @profile = current_user.build_profile end def create @profile = current_user.build_profiles(profile_params) redirect_to profiles_path end def show end def edit end def update @profile.update(profile_params) redirect_to(profiles_path(@profile)) end def destroy @profile.destroy redirect_to profiles_path end private def profile_params params.require(:profile).permit(:university, :degree) end def set_profile @profile = profile.find(params[:id]) end end when run rails server can submit form nothing getting stored in model 'profile'
here index.html.erb data should appear:
<h2>profiles</h2> <% @profiles.each |profile| %> <%= link_to profile %> <%= profile.university %> <% end %> <%= profile.degree %> <% end %> user.rb file:
has_one :profile and profile.rb file:
belongs_to :user nothing seems getting saved profile model , nothing getting displayed on index.html.erb. have created migration store user_id in profile model.
thank help
by far best way create profile user build when user object created:
#app/models/user.rb class user < activerecord::base has_one :profile before_create :build_profile accepts_nested_attributes_for :profile end #app/models/profile.rb class profile < activerecord::base belongs_to :user end this build blank profile each time new user created. means each user only ever have one profile, they'll able populate & edit.
in regards issue, there several points:
- don't list "profiles" index, list users & pull profile data
- if managing profile, nest under associative user model
here's how that:
# config/routes.rb resources :users, only: :index resource :profile, only: [:show, :update] #app/controllers/profiles_controller.rb class profilescontroller < applicationcontroller def show end def update redirect_to :show if current_user.update profile_params end private def profile_params params.require(:user).permit(profile_attributes: [:name]) end end #app/views/profiles/show.html.erb <%= form_for current_user, url: profile_path |f| %> <%= f.fields_for :profile |p| %> <%= p.text_field :name %> <% end %> <%= f.submit %> <% end %> update
my post above do.
the way works simple -- when user created (ie have gone trouble of filling out details), rails backend automatically creates blank profile object.
this several things:
always makes sure have
profileeach user (you don't have go bother of making them "create" profile).gives ability validate inputted data on created
profile(not having guess whether it's been done).
--
if you're getting undefined method build_profile, means associations incorrect.
all singular associations have build_[association] defined instance methods. code provided fires build_profile has_one association. time "undefined" if association plural
--
update
this suggests routing error.
considering appears @ root, think problem here:
#app/views/layouts/application.html.erb <%= link_to "profile", profile_path %> you don't have edit_profile_path -- should profile_path

Comments
Post a Comment