2016-04-19 69 views
0

我想呈現按鈕上的部分點擊,但沒有任何反應。
我的觀點:Rails:嘗試呈現按鈕上的部分點擊

<div class ="container"> 
    <span class ="center"><h1> Title</h1> 
    <%= link_to "Correct Answer", correct_answer_courses_path, :remote => true %> 
    <%= link_to "False Answer", false_answer_courses_path, :remote => true %> 

    <div id = "result"> 
     <!-- This Div is used to render result --> 
    </div> 
    </span> 
</div> 
<%= render 'layouts/coursefooter' %> 

我的控制器(Courses_Controller)

def correct_answer 
    respond_to do |format|    
    format.js 
    end   
end 

def false_answer 
    respond_to do |format|    
    format.js 
    end   
end 

correct_answer.js.erb(在課程文件夾)

$("#result").prepend('<%= escape_javascript(render :partial => "correct") %>'); 

我的部分被稱爲_correct並且也位於在課程文件夾中。 現在,當我點擊沒有任何反應,沒有任何錯誤。

+0

嘗試重命名correct_answer.js到correct_answer.js.erb –

+0

它已經更名爲.js.erb(我做出了錯誤的上市是錯誤的),但仍不起作用 – M1xelated

+0

試試像這樣:$('#result')。prepend(「#{j render'correct'}」); –

回答

2

我認爲這將有助於...

添加一個div與上述結果DIV ID correct_answer,並嘗試做這在js.erb文件,如:

$("#correct_answer").html("<%= j render partial: "correct", :locals => {:questionnumber => @questionnumber} %>"); 

並在控制器設置,如變量:

def correct_answer 
    @questionnumber = 2 
    respond_to do |format|    
    format.js 
    end   
end 
+0

工作!爲什麼我不能使用結果作爲標記? – M1xelated

+0

另外,我將如何傳遞變量。我試過這樣做 <%= link_to「Correct Answer」,correct_answer_courses_path,:remote => true,locals:{questionnumber:2}%> – M1xelated

+0

'locals:{questionnumber:2}'應該通過部分渲染語句傳遞: ''(「#correct_answer」)。html(「<%= j呈現部分:」正確「,當地人:{questionnumber:2}%>」);' –