次の例の解決策を試しました: In Rails, how do you render JSON using a view?
しかし、私の問題は、データベースに既に JSON 文字列が保存されていることです。json 文字列をプルすると、JSON ファイルをビューに表示できるはずです。
Androidタブレットは同じURLにアクセスできるはずですが、その設定(POSTで送信)に応じて別のJSONファイルを表示する必要があるため、これを使用しています.Androidタブレットはjsonをフェッチし、それを使用していくつかのオプションを設定します.
だから私はすでに完全に機能するjsonファイルを持っています。それをビューに表示する方法を探しています(htmlタグやその他のものをレンダリングせずに)。私は次のことを試しました(はい、respond_to :jsonを追加しました):
# def show_json (@config is the record, @config.json_config is the actual json configuration file
@config = event.et_relationships.where(terminal_id: terminal).first
respond_to do |format|
format.json
end
それから私が持っている私の見解
#show_json.json.erb
<%= @config.config_json %>
次に、表示される HTML (エラーは表示されません)
<html><head><style type="text/css"></style></head><body></body></html>
ありがとう!
編集
私はレール3.2.3を使用しています
これが私のルートです(関連する部分のみ)
match '/events/config', to: "events#show_json", as: :show_json
resources :events do
member do
get :select_item
get :category
end
end
次に、コントローラーも(部分的に)
respond_to :html, :json, :js
def show_json
#terminal_id = params["mac"]
terminal_id = "3D:3D:3D:3D:3D:3D"
event = current_user.events.where("date(endtime) > ? AND date(starttime) < ?", Time.now.to_s, Time.now.to_s).first
if event.nil?
# Nothing got returned so we use the default event
event = current_user.events.where('"default" = ?', true).first
end
logger.info "MAC: #{terminal_id}"
terminal = current_user.terminals.where(serial: terminal_id).first
logger.info "Terminal: #{terminal.attributes.inspect}"
logger.info "#{event.attributes.inspect}"
@config = event.et_relationships.where(terminal_id: terminal).first
logger.info "CONFIG #{@config[:config_json]}"
respond_to do |format|
format.json { render json: @config[:config_json] }
end
end