Ruby on rails 在rails中以JSON形式返回部分pg数据库条目
我有一个以postgresql作为数据库的RoR应用程序 目前,我有以下代码:Ruby on rails 在rails中以JSON形式返回部分pg数据库条目,ruby-on-rails,json,ruby,database,postgresql,Ruby On Rails,Json,Ruby,Database,Postgresql,我有一个以postgresql作为数据库的RoR应用程序 目前,我有以下代码: def return_mssg_as_json id = params[:id].to_i @message = Message.find(id) render json: @message end 它返回这个JSON: {"id":13,"created_at":"2017-10-27T19:03:52.196Z","updated_at":"2017-10-27T19:03:52.196
def return_mssg_as_json
id = params[:id].to_i
@message = Message.find(id)
render json: @message
end
它返回这个JSON:
{"id":13,"created_at":"2017-10-27T19:03:52.196Z","updated_at":"2017-10-27T19:03:52.196Z","text":"ASDF"}
如何使其仅返回
{“text”:“ASDF”}
只需使用以下命令:
呈现json:{message:@message[:text]}
只需使用以下命令:
呈现json:{message:@message[:text]}
您也可以使用来实现这种行为,在这里检查一个示例您也可以使用来实现这种行为,在这里检查一个示例这里有几件事
首先,您不需要在此处执行操作:
def return_mssg_as_json
id = params[:id].to_i
@message = Message.find(id)
render json: @message
end
您只需执行以下操作:
def return_mssg_as_json
@message = Message.find(params[:id])
render json: @message
end
现在,如果您可能收到一个没有匹配记录的id
,您可能需要执行以下操作:
def return_mssg_as_json
begin
@message = Message.find(params[:id])
render json: @message
rescue ActiveRecord::RecordNotFound
render json: {errors: :not_found}, status: :not_found
end
end
否则,如果您的记录不存在,您将有一个未处理的严重错误。但是,您可以这样做:
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: @message
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: { message: @message.text }
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
attr = @message.attributes.with_indifferent_access
render json: attr.slice(:foo, :bar).merge!(message: attr[:text])
else
render json: {errors: :not_found}, status: :not_found
end
end
现在,回到你的主要问题,你可以这样做(如公认的答案)
不过,为了保存三个字符,您似乎可以:
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: @message
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: { message: @message.text }
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
attr = @message.attributes.with_indifferent_access
render json: attr.slice(:foo, :bar).merge!(message: attr[:text])
else
render json: {errors: :not_found}, status: :not_found
end
end
这是一个很好的答案!但是,在将来,如果您想从@message
返回多个值(例如),您可以执行以下操作(假设@message
响应foo
和bar
):
此位:
@message.attributes
.with_indifferent_access
将记录转换为其值的哈希。此位:
.with_indifferent_access
允许您使用符号而不是字符串选择键值对。这一点:
.slice(:text, :foo, :bar)
指定要返回的键值对。因此,这将返回以下内容(当然是JSON格式):
现在,在您的示例中,您不想要文本
,而是想要消息
。因此,你可以:
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: @message
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: { message: @message.text }
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
attr = @message.attributes.with_indifferent_access
render json: attr.slice(:foo, :bar).merge!(message: attr[:text])
else
render json: {errors: :not_found}, status: :not_found
end
end
这将返回以下内容(自然是JSON格式):
这里有几件事
首先,您不需要在此处执行操作:
def return_mssg_as_json
id = params[:id].to_i
@message = Message.find(id)
render json: @message
end
您只需执行以下操作:
def return_mssg_as_json
@message = Message.find(params[:id])
render json: @message
end
现在,如果您可能收到一个没有匹配记录的id
,您可能需要执行以下操作:
def return_mssg_as_json
begin
@message = Message.find(params[:id])
render json: @message
rescue ActiveRecord::RecordNotFound
render json: {errors: :not_found}, status: :not_found
end
end
否则,如果您的记录不存在,您将有一个未处理的严重错误。但是,您可以这样做:
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: @message
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: { message: @message.text }
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
attr = @message.attributes.with_indifferent_access
render json: attr.slice(:foo, :bar).merge!(message: attr[:text])
else
render json: {errors: :not_found}, status: :not_found
end
end
现在,回到你的主要问题,你可以这样做(如公认的答案)
不过,为了保存三个字符,您似乎可以:
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: @message
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: { message: @message.text }
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
attr = @message.attributes.with_indifferent_access
render json: attr.slice(:foo, :bar).merge!(message: attr[:text])
else
render json: {errors: :not_found}, status: :not_found
end
end
这是一个很好的答案!但是,在将来,如果您想从@message
返回多个值(例如),您可以执行以下操作(假设@message
响应foo
和bar
):
此位:
@message.attributes
.with_indifferent_access
将记录转换为其值的哈希。此位:
.with_indifferent_access
允许您使用符号而不是字符串选择键值对。这一点:
.slice(:text, :foo, :bar)
指定要返回的键值对。因此,这将返回以下内容(当然是JSON格式):
现在,在您的示例中,您不想要文本
,而是想要消息
。因此,你可以:
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: @message
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
render json: { message: @message.text }
else
render json: {errors: :not_found}, status: :not_found
end
end
def return_mssg_as_json
if @message = Message.find_by(id: params[:id])
attr = @message.attributes.with_indifferent_access
render json: attr.slice(:foo, :bar).merge!(message: attr[:text])
else
render json: {errors: :not_found}, status: :not_found
end
end
这将返回以下内容(自然是JSON格式):
但是如何将其作为类似json的{“text”:“ASDF”}返回。您的解决方案只返回消息。是的,但是如果我用“string”测试它,它应该返回json{“message”:“string”}。您的解决方案只返回字符串。但是如何将其作为类似于json的{“text”:“ASDF”}返回。您的解决方案只返回消息。是的,但是如果我用“string”测试它,它应该返回json{“message”:“string”}。您的解决方案只返回字符串。这也是一个很好的解决方案,但前面的注释在不使用额外gems的情况下完成了所有必要的操作。这也是一个很好的解决方案,但前面的注释在不使用额外gems的情况下完成了所有必要的操作。