2014-03-27 125 views
0

在我的TestimoniesController中我寫了一條render js:行,如果@testimony.save不成功。Rails 4渲染js:重定向到另一個頁面並顯示原始js

if @testimony.save 
    redirect_to @testimony 
else 
    render js: "alert('Please make sure you feel out every field of the Testimonial!');" 
end 

不用翻譯,我寫了警報,它重定向我到/testimonies路線,只是顯示應該被渲染JavaScript和沒有其他的字符串。我的JS被禁用了嗎?

這裏是我的new.html.erb文件:

<h1> New Testimonial</h1> 
<%= form_for :testimony, url:testimonies_path do |f| %> 
    <p> 
    <%= f.label :first_name %><br> 
    <%= f.text_field :first_name %> 
    </p> 
    <p> 
    <%= f.label :last_name %><br> 
    <%= f.text_field :last_name %> 
    </p> 
    <p> 
    <%= f.label :email %><br> 
    <%= f.text_field :email %> 
    </p> 
    <p> 
    <%= f.label :contact_number %><br> 
    <%= f.text_field :contact_number %> 
    </p> 
    <p> 
    <%= f.label :country %><br> 
    <%= f.text_field :country %> 
    </p> 
    <p> 

    <!--bunch of the same stuff --> 
    <p> 
    <%= f.submit %> 
    </p> 
<% end %> 
+0

你加'遙控:TRUE'在你的鏈接,點擊該控制器動作 – Abk

+0

分享您的HTML文件以及 – Abk

+0

我認爲你應該使用的方法和表演js.erb文件對用戶的迴應。 –

回答

1

爲了TOR與JS迴應,你必須遠程添加:真正在你的form_for,並提交您的請求作爲AJAX

<%= form_for :testimony, url:testimonies_path, remote: :true do |f| %> 

但那麼你的

redirect_to @testimony 

將無法​​正常工作,因爲它只適用於html格式,所以在這種情況下重新將與該行

render js "location.href = '#{testimony_path}'"; 
相關問題