Json – How to render JSON in Rails view

jsonruby-on-rails-3

I've tryed the solution of following example: In Rails, how do you render JSON using a view?

But my problem is that the database already has a JSON string saved, I pull the json string and then I should be able to display the JSON file in the the view.

I'm using this because an android tablet should be able to visit the same url but depending on its settings (send by a POST) a different JSON file should be displayed, the android tablet then fetches the json and use it to configure some options.

So I already have a full working json file, i'm looking for a way to display it in a view (without rendering html tags and other stuff). I tryed the following (yes I've added 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

Then my view I have

#show_json.json.erb
<%= @config.config_json %>

Then the HTML I get to see (no errors are given)

<html><head><style type="text/css"></style></head><body></body></html>

Thanks!

EDIT

I'm using rails 3.2.3

Here is my routes (only relevant parts)

match '/events/config', to: "events#show_json", as: :show_json
resources :events do
    member do
      get :select_item
      get :category
    end
  end

Then also the controller (partial)

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

Best Answer

Use render:

@config = event.et_relationships.where(terminal_id: terminal).first
respond_to do |format|
   format.json { render json: @config }
end

And then you have path /:model/:action.json.