如何使用ajax加载部分

时间:2015-10-28 15:31:31

标签: jquery ruby-on-rails ajax ruby-on-rails-3 ruby-on-rails-4

实际上,尽管将ajax加载设置为' remote => true',但它会加载网址

  

' / noajax_en / V / YT /' + @ movie.uid +' / refresh_part_after_comment'

为什么它不能加载ajax?它应该加载下面的网址

  

' / EN / V / YT /' + @ movie.uid +' / refresh_part_after_comment'

这是我的代码

查看

<%= form_for(@comment, :url => {:controller => "comments", :action => "create" }, :remote => true) do |f| %>
    <%= f.text_field :body, id:"body_input" %>
    <%= f.hidden_field :elapsed_time, id: "elapsed_time" %>
    <%= f.hidden_field :video_id, value: params[:uid] %>
    <%= button_tag( :class => "btn btn-primary") do %>
        Post
    <% end %>                   
<% end %>

comments_controller.rb

def create
    .
    .
    .
    flash[:notice] = "posted"       

    if request.xhr?  # ajax request
        respond_to do |format|
            render '/en/v/yt/' + @movie.uid + '/refresh_part_after_comment'
        end
    else
            redirect_to '/noajax_en/v/yt/' + @movie.uid + '/refresh_part_after_comment'
    end
end

1 个答案:

答案 0 :(得分:1)

错误的事情......

form_for needs:remote:选项中的true

在您的控制器中

..无需检查请求对象.. respond_to已经这样​​做了:

respond_to do |format|
  format.html {# redirect here}
  format.js do
    render 'view name.js.erb'  # or leave out to render default view.js.erb for action
  end
end