简体   繁体   中英

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 = "<%=@question.title %>";
document.getElementById("txt_tag").innerHTML = "<%=@question.tag %>";
$(".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) 

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)

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM