Ruby on Rails修改而不是编辑

时间:2011-02-22 18:31:54

标签: ruby-on-rails ruby controller devise

我对RoR仍然有点新,并且正在使用脚手架为数据生成CRUD接口。

我正在使用设计进行用户身份验证,并希望允许拥有特定条目的用户进行编辑或删除,但要保护其他用户的数据。但是,我想允许其他用户修改或创建新版本。

因此,如果尝试编辑的用户看起来好像正在编辑,但是当他们提交时,控制器应该实际生成一个新条目(并可能指定它派生的条目的parent_id)。

非常感谢任何有关实施的帮助。

2 个答案:

答案 0 :(得分:2)

另请查看Ancestry,它是一个非常好的库,可以帮助进行版本控制。

答案 1 :(得分:0)

这是我提出的解决方案。我是一个ruby newb,所以我假设我可以用相同的参数调用create函数,但不知道该怎么做,所以我只是重复代码:

  def update
    @section = Section.find(params[:id])

    if @section.owner == current_user.id
      respond_to do |format|
        if @section.update_attributes(params[:section])
          format.html { redirect_to(@section, :notice => 'Section was successfully updated.') }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @section.errors, :status => :unprocessable_entity }
        end
       end
     else
        # REVISE 
        @childsection = Section.new(params[:section])

        respond_to do |format|
            if @childsection.save
                format.html { redirect_to(@childsection, :notice => 'Section was successfully revised.') }
                format.xml  { render :xml => @childsection, :status => :created, :location => @childsection }
            else
                format.html { render :action => "new" }
                format.xml  { render :xml => @childsection.errors, :status => :unprocessable_entity }
            end
        end
     end
  end