使用Rails 3应用程序的respond_with语法的奇怪行为?

Aut*_*act 6 ruby ruby-on-rails-3

在为json服务的Rails 3.0.3应用程序构建api时,会发生一些意想不到的行为.

以下是控制器.问题是关于respond_with.我已经respond_to :json在app控制器中了.

create动作正常工作,数据也会在创建后发回.

但更新操作respond_with不会发回任何数据.

响应正文是空白的.

def create
  line = get_line
  input_header = line.input_headers.create(params[:input_header])
  respond_with(input_header, :location => api_v1_line_input_header_url(line,input_header))
end

def show
 input_header = get_input_header
 respond_with(input_header.to_json)
end

def update
  input_header = get_input_header
  input_header.update_attributes(params[:input_header])

  respond_with(input_header, :location => api_v1_line_input_header_url(input_header.line,input_header))

  # render :json => input_header
end
Run Code Online (Sandbox Code Playgroud)

当我使用render :json => input_header而不是respond_with,它的工作原理.为什么是这样?

Ant*_*ton 0

也许您忘记附加到控制器respond_to?

respond_to :json, :html, ...
Run Code Online (Sandbox Code Playgroud)