ROR:HTML和JSON都有?

时间:2013-08-16 15:12:25

标签: html ruby-on-rails ruby json

Scaffolding创建具有Create / Update方法的控制器。这些方法具有呈现HTML和JSON。我知道HTML,但我不知道JSON是什么。是否有必要在那里使用JSON,或者我可以将其取出并仍然可以使用HTML呈现?

以下是我要讨论的代码:

def create
  @judge = Judge.new(judge_params)

  respond_to do |format|
    if @judge.save
      format.html { redirect_to @judge, notice: 'Judge was successfully created.' }
      format.json { render action: 'show', status: :created, location: @judge }
    else
      format.html { render action: 'new' }
      format.json { render json: @judge.errors, status: :unprocessable_entity }
    end
  end
end

2 个答案:

答案 0 :(得分:1)

如果您对JSON不感兴趣,可以这样做:

def create
  @judge = Judge.new(judge_params)

  if @judge.save
    redirect_to @judge, notice: 'Judge was successfully created.'
  else
    render action: 'new'
  end
end

答案 1 :(得分:0)

是的,您可以删除JSON部分并仍然可以正常工作。

JSON是一种常用于Javascript / API调用的流行格式。