create.json.jbuilder没有返回对象?

时间:2016-01-11 05:11:53

标签: ruby-on-rails jbuilder

def create
    @practice = Practice.new(practice_params)

    if @practice.save && @practice.start
      respond_to do |format|
        format.html { redirect_to practice_learnings_path(@practice) && return }
        format.json
      end
    else
      redirect_to practices_path
    end
  end

//create.json.jbuilder
json.practice @practice

1 个答案:

答案 0 :(得分:0)

您需要为此create操作创建新的视图文件,即create.json.jbuilder下的views/pratices。在那里写你的代码以返回json。

E.g:

# app/views/practices/create.json.jbuilder

json.practise do
  json.attr1 @practice.attr1
  json.attr2 @practice.attr2
  json.attr3 @practice.attr3
end

请参阅此{/ p>的jbuilder gem doc more information