2013-07-13 95 views
0

對於我的Rails應用程序,我有一個表單,可以在提交時在模型中保存「內容」。如何在下面修改我的腳本並使用「會話」以便「onSuccess(event)」,我將該警報的內容作爲臨時值分配給:content,並且我的頁面將在text_area內重新加載/刷新警報的內容?Rails - 如何通過腳本爲表單賦值並使用賦值重載它?

<%= form_for([@project, @project.blogs.build]) do |f| %> 
    <div class="field"> 
    <%= f.text_area :content, label: "Blog", :class => "redactor" %> 
    </div> 
    <%= f.submit "Submit" %> 
<% end %> 

<script> 

    ... 

    function onSuccess(event) { 
     alert('NICE! ' + event.data.Id + ' was added.'); 
    } 

    ... 

</script> 

回答

0

沒有與阿賈克斯

<%= form_for [@project, @project.blogs.build], :remote => true, :html => { :id => "ajax_form" } do |f| %> 
    <div class="field"> 
    <%= f.text_area :content, label: "Blog", :class => "redactor", :id => "change_me" %> 
    </div> 
    <%= f.submit "Submit" %> 
<% end %> 

<script> 

    $(function() { 
     $("#ajax_form").bind('ajax:success', function(data, status, xhr) { 
     $("#change_me").val("changed"); 
     }).bind('ajax:error', function(xhr, status, error) { 
     console.log(error); 
     console.log(status); 
     }); 
    }); 

</script> 
+0

我試過這個,但頁面不刷新我的redactor rails窗體中的event.data.ID值。你知道爲什麼嗎? – spl

+0

@spl,不是一回事。也許你需要什麼。 –

0
如果使用redactor.js

的選項。

function onSuccess(event) { 
    $('.redactor').redactor('insertText', 'NICE! ' + event.data.Id + ' was added.'); 
}