如何使用AJAX重新加载与Rails控制器操作无关的页面部分 [英] How to use AJAX to reload a section of page unrelated to rails controller action

查看:49
本文介绍了如何使用AJAX重新加载与Rails控制器操作无关的页面部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序遵循前面的问题

解决方案

到目前为止,临时解决方案(可能不建议使用,但仍会在工作编程实践中使用)是在特定条件下隐藏和取消隐藏某些元素.

在用户交互(例如 onClick )时,在更新类似于控制器更新的值时取消隐藏和隐藏元素.只需模拟刷新并更新它们指向的表单路径/控制器操作即可.

在布尔复选框形式下,here and here.

See the picture below of the interface.

What I want is, when the user edits/updates verification through the switch icon (submit button automatically), also described here, the div containing the verify/patch icon should reload/refresh to update that a translation is verified.

I could possibly have all these elements in one field, but only moderator: true users get to create and only moderators who moderated specific translations can edit and update the review.

What I tried is having

# entries/index.html.erb
<% @entries.each do |entry| %>
  ...

  <span id="show-review-<%= entry.id %>">
    <%= render 'reviews/show', entry: entry %>
  </span>

  ...

  <% if current_user.moderator %> # and other conditions
    <div id="edit-review-form-<%= entry.id %>">
      <%= render 'reviews/edit', entry: entry %>
    </div>
  <% end %>

  ...
<% end %>

# reviews/_show.html.erb
# Simply if the review exists and if its true
<% if entry.review and entry.review.verified == true %>
  # blue verify icon
<% else %>
  # gray vefify icon
<% end %>

# reviews/_edit.html.erb
<%= form_for([entry, entry.review], remote: true, :authenticity_token => true) do |f| %>
   ...
<% end %>

# reviews/edit.js.erb
$('show-review-<%= entry.id %>')[0].html("<%=j render 'reviews/show', entry: entry %>")


Problem: When I submit the edit form, and update action in the controller is executed, I would like to reload the section of show. If I have my controller in this way...

def update
  if @entry.review.update!(review_params)
    respond_to do |format|
      format.html { render :partial => "reviews/show", :layout => false }
      format.js { render :template => "reviews/edit", :layout => false }
    end
  end
end

Having this, on update, my site instead redirects to a reviews URL http://localhost:3000/entries/entry:id/reviews/review:id

This is a nested, kinda, model. Why does is redirect to show when it's just a partial?

# rake routes
entry_reviews     GET    /entries/:entry_id/reviews(.:format)             reviews#index
                  POST   /entries/:entry_id/reviews(.:format)             reviews#create
new_entry_review  GET    /entries/:entry_id/reviews/new(.:format)         reviews#new
edit_entry_review GET    /entries/:entry_id/reviews/:id/edit(.:format)    reviews#edit
entry_review      GET    /entries/:entry_id/reviews/:id(.:format)         reviews#show

解决方案

So far the temporary solution, not probably recommended but still does the job programming practice, is hiding and unhiding certain elements on specific conditions.

Upon user interaction (e.g onClick), unhide and hide elements while updating values that resemble the controller update. Simply simulate a refresh and update the form paths/controller actions they point to.

This will mostly work better with boolean check-box forms, likes or dislikes where variables will only alternate between two instances at max. Hence you don't have to write much javascript to handle pseudo outputs.

But still, I'm sure there's a better method for example WebSockets through Action Cable. That can probably be overkill but the closest to realtime updates. respond_to just doesn't seem to work

这篇关于如何使用AJAX重新加载与Rails控制器操作无关的页面部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆