Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:36:46+00:00 2026-06-18T14:36:46+00:00

I have Vacancies controller and I need to pass @vacancies to json and also

  • 0

I have Vacancies controller and I need to pass @vacancies to json and also render another layout. The following code does not work (json is not passed however I have “wide” layout). If I remove format.html { render layout: “wide”} } json passes correctly. How to combine these two things?

class VacanciesController < ApplicationController
respond_to :html, :json
...
    def index
      @vacancies = Vacancy.all
      respond_with(@vacancies) do |format|
       format.html { render layout: "wide"} }
       format.json { render json: @vacancies } 
      end
     end
  ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:36:47+00:00Added an answer on June 18, 2026 at 2:36 pm

    You can’t call render twice, that’s problem #1. You also can’t send two responses to a single request.

    There is also no purpose in rendering HTML (which means a fresh page load) and sending JSON (which is for AJAX requests, ie requests that don’t reload the page) at the same time. It isn’t possible, but it also would be pointless even if it was possible.

    If you want to tell a request to use a specific layout, you can pass the layout option to a render call. However, a render call does not take a data object as the first argument, it takes a view name, or only an options hash. So to call this correctly you should use:

    render :index, :layout => 'example'
    

    I expect that will make your HTML views show up correctly.

    Please understand however, the layout option is only useful for HTML responses, not JSON responses. Layout is telling your render call what outer HTML to wrap around the view your action is calling, and if you don’t specify it uses ‘application.html’

    To help you understand one more thing: your respond block is telling the computer how to respond to different kinds of requests. It’s like a switchboard. If you wrote it with if/else statements it might look like this:

    if request_type == 'html'
      render :index, :layout => 'wide'
    elsif request_type == 'json'
      render :json => @vacancies
    else
      raise raise ActionController::UnknownFormat
    end
    

    So, with your respond_with block, if you fix your html render call, and assuming you’re developing on localhost, if you enter the following URL in your browser and hit enter…

    http://localhost:3000/vacancies
    

    That would be making an HTML format GET request, which will load the page with layout: 'wide' but no other data. If you type:

    http://localhost:3000/vacancies.json
    

    That will simulate a JSON request, and you’ll get just the JSON representation of the @vacancies data.

    I hope that helps you solve your problem. If not, please describe what you’re trying to accomplish in more detail so I can help you understand how to do it.

    PS: one last tip: if you want to specify layouts at the controller level you can just call layout at the top of your controller, like so:

    class ExampleController < ApplicationController
      layout 'awesome', :only => [:new,:edit]
      ...
    end
    

    This works like any other filter, you can pass :only, or :except, or no options at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To improve performance, I have written the following: a = Address.joins(:vacancies).select('addresses.city, COUNT (*) AS
Have the following code: $(#blogs).mouseover( function () { $(this).addClass(hover); $(#home).removeClass(hover); $(#homepages).removeClass(hover); $(#apps).removeClass(hover); $(#facebook).removeClass(hover); $(#kontakt).removeClass(hover);
Have the following code. The height is variable so the height of the floating
Ok I have a sliding panel on my website (http://www.security-vacancies.net/index.php) when the link view
I have 3 tables: Countries: Code, Name Vacances: id(autoincrement),CountryCode, NameOfCountry, numberOfdays How to foreach
Currently I have a database object, vacancies which has a column (foreign key) CareerLevels.
I have a jboss server running, and inside webapplication there code below. The question
Im setting the the datasource with the following code: protected void Page_Load(object sender, EventArgs
I have the PHP code as below: <?php $htmlFile = file_get_contents(http://archi-graphi.com/arcancianev/sejour-29-eau_turquoise_en_corse.html'); $pdfHtml = ('pdfFile.html');
Have a TSQL view, which I need to group by one column, however, am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.