2

I basically have an action that because of logic needs to return with the contents of another js file. How do I go about doing this? Thanks

app/controllers/classrooms_controller.rb

def create
  if params[:test_logic]
    respond_to do |format|
      format.js { render 'create_differently' } # This doesn't work.
    end
  else
    redirect_to root_path
  end
end

app/views/classrooms/create_differently.js.erb

alert('hi')
4
  • Its showing this in the backend: Processing by ClassroomsController#create as HTML Why would that happen if I specifically told it to return js? Commented Jan 5, 2013 at 1:45
  • That message appeared since you have requested HTML page and is printed before controller code is executed. So what is the result of that call? What do you see in browser? Is there an exception? Commented Jan 5, 2013 at 1:50
  • It sends me to a blank page (checked the source). Why is it sending me to a blank page with errors and in HTML? Commented Jan 5, 2013 at 2:02
  • how is the url, you are calling? remember, if you call localhost:3000/classrooms/1 a request for html will be made. Only localhost:3000/classrooms/1.js will render to javascript. Commented Jan 6, 2013 at 0:24

1 Answer 1

6

You need to add

 :layout => false 

to avoid the rendering of the html layout for your js file.

Additionally you could define the different js-file like this

:template => "classrooms/create_differently.js.erb"

both together:

 format.js { 
    render :template => "classrooms/create_differently.js.erb", 
           :layout => false  
 }

For browser-based testing, please be aware calling js not html!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.