【问题标题】:create.json.jbuilder not returning object?create.json.jbuilder 不返回对象?
【发布时间】:2016-01-11 05:11:53
【问题描述】:
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

【问题讨论】:

  • Vivek,欢迎来到 Stackoverflow :) 请解释您的问题。仅显示您的代码并不是提出问题的正确方法。请查看how to ask a question

标签: ruby-on-rails jbuilder


【解决方案1】:

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

例如:

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

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

请参阅jbuilder gem 文档以获取有关此内容的more information

【讨论】:

    猜你喜欢
    • 2021-05-28
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    相关资源
    最近更新 更多