ruby on rails - editing in place for a has one relationship -
how can utilize of edit in place gems par example: best_in_place, edit properties of kid model in has_one , or has_many relationships.
example: (just example, not actual issue; used explanation.) user model has many goals (goal model) , each goal there properties. how utilize gem 'best_in_place' edit these properties show page.
tried show in "in-place editing" on railscast, http://railscasts.com/episodes/302-in-place-editing, ended errors such "undefined method :property goal"
tried @user.goal
in spot model class no avail.
actually code can seen below.
i'm using edit user's profile attributes similar concepts , errors..
user.rb
class user < activerecored::base has_one :profile, dependent: :destroy after_create :build_profile accepts_nested_attributes_for :profile attr_accessible :email, :password, :password_confirmation, :profile_attributes def build_profile profile.create(user_id: self.id) end end
profile.rb
class profile < activerecord::base attr_accessible :name. :address, :age belongs_to :user validates :name, presence: true, length: { maximum: 50 } validates :address, :age, presence: true end
users_controller.rb
class userscontroller < applicationcontroller def new @user = user.new @profile = self.build_profile end def create @user = user.new(params[:user]) if @user.save sign_in @user flash[:success] = "welcome, signing up" redirect_to @user else render 'new' end end end
profiles_controller.rb
class profilescontroller < applicationcontroller def edit @profile = profile.find(params[:id]) end def update @profile = profile.find(params[:id]) if @profile.update_attributes(params[:profile]) flash[:success] = "profile updated" redirect_to @user else render 'edit' end end def show @user.profile = user.find(params[:id]).profile end def destroy end end
user/show.html.erb
<% provide(:title, "profile" ) %> <h1><%= @user.profile.name %></h1> <p> <b> address </b> <%= best_in_place @profile, :address, :path => user_profile_path(@user, @profile) %> </p>
database tables (with user_id foreign key )
user table profile table ------------------------ ------------------------ | id | ---------- | id | ------------------------ | ------------------------ | email | ------------------ | user_id | ------------------------ ------------------------ | password | | name | ------------------------ ------------------------ | password confirmation | | address | ------------------------ ------------------------ | remember token | | age | ------------------------ ------------------------ | created @ | | created @ | ------------------------ ------------------------- | updated @ | | updated @ | ------------------------ -------------------------
you need add together update
action profiles_controller
something this:
def update @profile = profile.find params[:id] respond_to |format| if @profile.update_attributes(params[:profile]) format.json { respond_with_bip(@profile) } end end end
ruby-on-rails ruby
No comments:
Post a Comment