Rails 3 - 多表单向导 - 尝试编辑记录不保存值

时间:2011-07-13 23:25:47

标签: ruby-on-rails-3 forms

我正在按照Ryan Bates提供的步骤执行多表单向导。创建新记录有效,因此我在编辑记录时尝试使用相同的逻辑。但是,我更改的值不会更改 - 当我从第一个表单编辑某些内容时,向前然后向后,我的编辑不会保存。这是我的控制器中的代码:

  def edit
    session[:edit] = "Only change the fields you wish to edit"
    @demographic = Demographic.find(params[:id])
    session[:demographic_params] ||= {}  
  end

  def update
    session[:demographic_params].deep_merge!(params[:demographic]) if params[:demographic]  
    @demographic = Demographic.find(params[:id])
    @demographic.current_step = session[:demographic_step]
    if params[:back_button]
      @demographic.previous_step
    elsif @demographic.last_step?
      @demographic.update_attributes(params[:demographic])
      updated = true 
    else
      @demographic.next_step
    end    
    session[:demographic_step] = @demographic.current_step
    if not updated
      render "edit"
    else
      session[:demographic_params] = session[:demographic_step] = nil
      flash[:notice] = "Entry entered successfully"
      redirect_to demographic_path
    end
  end

我应该更改哪些内容可以保存修改内容?

1 个答案:

答案 0 :(得分:1)

我不知道这是否有效,但我认为应该是这样的,以节省每一步“改变”:

  def update
    session[:demographic_params].deep_merge!(params[:demographic]) if params[:demographic]  
    @demographic = Demographic.find(params[:id])
    @demographic.current_step = session[:demographic_step]
    @demographic.update_attributes(params[:demographic])
    if params[:back_button]
      @demographic.previous_step
    elsif @demographic.last_step?
      updated = true 
    else
      @demographic.next_step
    end    
    session[:demographic_step] = @demographic.current_step
    if not updated
      render "edit"
    else
      session[:demographic_params] = session[:demographic_step] = nil
      flash[:notice] = "Entry entered successfully"
      redirect_to demographic_path
    end
  end

即,将@demographic.update_attributes outsite移动到“逐步”逻辑。

所以,我认为你应该走这条路来解决你的问题。

希望这有帮助。