Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: render text gives missingtemplate error

I am trying to do an AJAX call with Rails. The call is to the change_profile action in my controller. The contents of this action are as follows:

def change_profile
    @test = params[:test]
    puts "AAAAAAA #{@test}"
    respond_to do |format|
       format.html
       format.js
    end
    render(:text => "FINISHED THE AJAX REQUEST")
end

When I call this, however, the Rails console says:

ActionView::MissingTemplate (Missing template main/change_profile  ....

I don't understand why this is happening. Since I'm rendering text, shouldn't it know not to try to find the template and just render the text?

like image 776
Andrew Latham Avatar asked Dec 31 '25 19:12

Andrew Latham


1 Answers

When left without a {}, the respond_to block looks for a file called change_profile.erb.js or some other change_profile.xxx.js. Add your code within the respond_to block

def change_profile
    @test = params[:test]
    puts "AAAAAAA #{@test}"
    respond_to do |format|
       format.html
       format.js {
         render :text => "FINISHED THE AJAX REQUEST"
       }
    end
end
like image 164
coneybeare Avatar answered Jan 03 '26 14:01

coneybeare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!