对多个控制器操作使用的表单进行Rails验证

时间:2012-01-13 20:07:30

标签: ruby-on-rails forms split

使用此代码进行下面的编辑和更新操作。我面临2个问题

  1. 如何确定提交的表格(基本,照片,兴趣或详情?)

  2. 我已经设置了validates_presence_of所有配置文件字段,但是这个表单通常用于两个控制器操作(因此不是每个属性都匹配)并且我得到错误。

  3. 我该如何解决这个问题?

    def edit 
      def sub_layout
        "left"
      end
    
      @profile = Profile.find params[:id]
      #authorize! :update, @profile
    
      what = params[:what]
      if ["basics", "location", "details", "photos", "interests"].member?(what)
        render :action => "edit_#{what}"
      else
        render :action => "edit_basics"
      end
    
    end
    
    def update
      @profile = Profile.find(params[:id])
    
      respond_to do |format|
        if @profile.update_attributes(params[:profile])
          format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
        else
          format.html {
    
            #there was an error,
            #redirect to the correct "what" form /edit/basics for example
            # what is empty how to get the value or a better way for this?
    
            what = params[:what]
            render :action => "edit_#{what}"
    
          }
    
    
        end
      end
    end
    

0 个答案:

没有答案