user2493476
user2493476

Reputation:

Rails rendering partial on ajax success

Hello guys I am developing an application with ruby on rails I want to render partial on ajax success Here is the action of the controller

def re
    @question = Question.find_by_title(params[:qt])
    respond_to do |re|
      re.js {}
    end
  end

And here is my re.js.erb:

document.getElementById("details").value = "<%= @question.details %>";
document.getElementById("tag").value ="<%= @question.tag %>";
var ansForm =document.getElementById("new_answer");
ansForm.action=" http://localhost:3000/questions/<%= @question.id %>/answers" ;
document.getElementById("txt_title").innerHTML = "<%[email protected] %>";
document.getElementById("txt_tag").innerHTML = "<%[email protected] %>";
$(".answers").empty();
$(".answers").append("<h1>Added Answers</h1>");
$(".answers").append("<%= render @question.answers %>");

But I am getting this error :

GET http://localhost:3000/re?qt=Can%20I%20add%20anything%20to%20water? 500 (Internal Server Error) 

Upvotes: 1

Views: 260

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Your code looks okay, but there'll be a problem which isn't shown publicly:

  1. Right-Click > Inspect Element > Network
  2. When you send the request, click on the new item that shows
  3. It should appear red -- click onto it & click on preview
  4. See what the "real" error is

The problem you'll have will likely be down to an error within your code (either Rails or JS). The only way to know for sure is to show the rendered error message in your development console

Try this:

  #controller
  def re
    @question = Question.find_by_title(params[:qt])
    respond_to do |format|
      format.js
    end
  end

  #re.js.erb
  alert("hello");

Upvotes: 0

gernberg
gernberg

Reputation: 2617

Check your serverlog (log/development.log) as there should be more information there than just the "500 Internal" that the browser gives you.

How does your config/routes.rb look like? (perhaps it doesnt accept GET to /re)

Upvotes: 1

Related Questions