ruby on rails - nested routes and form_for and models using has_one and belongs_to -


how map out has_one model nested routes , how add form_for /localhost:3000/users/1/profile/new,html.erb following restful database?

user has 1 profile.

models

class profile < activerecord::base   attr_accessible :name, :surname   belongs_to :user end  class user < activerecord::base   attr_accessible :email, :email_confirmation, :password, :password_confirmation   has_secure_password   has_one :profile, dependent: :destroy end    resources :users     resources :profiles      (note: has_one profile)     resources :progress_charts     resources :calories_journals   end 

views/profiles/new.html.erb

<h1>about you</h1> <div class="row">   <div class="span6 offset3">     <%= form_for(@profile) |f| %>     <%= render 'shared/error_messages' %>        <%= f.label :name %>       <%= f.text_field :name %>        <%= f.label :surname %>       <%= f.text_field :surname %>        <%= f.submit "create account", class: "btn btn-large btn-primary" %>     <% end %>   </div> </div> 

controller: profiles_controller.rb 2 errors have discovered not quite understand why wasn't working.

class profilescontroller < applicationcontroller def index end  def show end    def new   #  @user = user.find(params[:id]) # error message: couldn't find user without id   #  @profile = @user.build_profile()      @profile = current_user.build_profile(params[:id]) # error message: unknown attributes: user_id   end    def edit   end    def create   end    def update   end    def destroy   end end 

helpers: sessionhelper (to illustrate current_user) module sessionshelper def sign_in(user) cookies.permanent[:remember_token] = user.remember_token self.current_user = user end

  def signed_in?     !current_user.nil?   end    def current_user=(user)     @current_user = user   end    def current_user?(user)     user == current_user   end    def current_user     @current_user ||= user.find_by_remember_token(cookies[:remember_token])   end    def sign_out     self.current_user = nil     cookies.delete(:remember_token)   end    def signed_in_user     unless signed_in?       store_location       redirect_to signin_path, notice: "please sign in."     end   end end 

does profiles table has user_id attribute?

in routes, profile should singular, since user has 1 profile:

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

the route user profile users/:user_id/profile (and not users/:user_id/profile/:id

in profiles_controller:

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

and form like:

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

do want user create profile that? usually, create profile when user register.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -