簡體   English   中英

ROR:從一種形式更新兩個模型

[英]ROR: Updating two models from one form

我的表單上有兩個模型配置文件和用戶。 創建用戶后,他可以繼續編輯其個人資料。 意見很好。 但是,當我單擊“保存”以更新編輯的配置文件時。 它沒有更新,但是閃存通知顯示該配置文件已更新。 可能是什么問題? 我不確定出了什么問題。 下面是代碼。

class ProfilesController < ApplicationController

  def new
    #@user.profile = Profile.new
    @user = User.find(current_user.id)
    @identity = @user.profile || @user.build_profile()
    @identity.save
  end

  def update
    @user = current_user
    @identity = @user.profile
    if @identity.update_attributes(params[:identity])
      flash[:notice] = 'Profile was successfully updated.'
      redirect_to(new_profile_path())
    else
      render :action => "new"
    end
  end
end


class UsersController < ApplicationController

  def show
    @user = current_user
    @user = User.find(params[:id])
    @identity = @user.profile || @user.build_profile()
    @identity.save
  end

......

end

謝謝你的協助。

這里可能存在一些錯誤。 但是,解決此問題的最佳方法是簡化並使用內置的rails功能來編輯關聯。

我建議做的是使用嵌套屬性, Ryan Daigle上有一篇很棒的文章

我不確定為什么您會在一個new操作中而不是在create調用save ,這感覺不對。 還要檢查您提交的表單中的模型名稱是否是identity而不是userprofile

沒有配置文件的用戶可以存在嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM