【问题标题】:where do I put the respond_to if there is an if-statement in the controller in rails?如果rails的控制器中有if语句,我应该把respond_to放在哪里?
【发布时间】:2010-06-10 04:49:01
【问题描述】:

我有一个具有 if 条件的控制器:

  def update
    @contact_email = ContactEmail.find(params[:id])

    if @contact_email.update_attributes(params[:contact_email])
      flash[:notice] = "Successfully updated contact email."
      redirect_to @contact_email
    else
      render :action => 'edit'
    end
  end

respond_to 块应该放在哪里:

respond_to do |format| 
  format.html {}
  format.json {render :json =>@contact_email}
end

【问题讨论】:

    标签: ruby-on-rails json respond-to


    【解决方案1】:
    def update
      @contact_email = ContactEmail.find(params[:id])
      ok = @contact_email.update_attributes(params[:contact_email])
    
      respond_to do |format| 
        format.html {
          if ok
            flash[:notice] = "Successfully updated contact email."
            redirect_to @contact_email
          else
            render :action => 'edit'
          end
        }
        format.json {
          if ok
            render :json =>@contact_email
          else
            ...
          end
        }
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2016-01-28
      • 1970-01-01
      • 2020-09-30
      • 2016-05-13
      • 2016-08-25
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多