开发者

update the div in the loop object variable

I have one page in view side of my application where there is a loop of the 开发者_Go百科object @users now i want to update only one part of the div and that div is inside the loop i have made div dinamic but i want to update that div , in the same page. I have used partial page to update the page by controller , but that is not wrking due to div is in loop . my code is view side :

<% for user in @users %>
    <% if !user.country.blank?%> <%= user.country.to_s.capitalize  %><%end%>
    <div id="div_state_<%=user.id%>">
             <%= user.state  %>
              <%if user.state == 'active'%>
              <%= link_to_remote image_tag("/images/de-active.png",:title =>" make it pending"), :url => admin_manage_user_state_change_path(user.id),:update => 'div_state_#{user.id}'  %>
              <%else%>
              <%= link_to_remote image_tag("/images/active.png",:title =>" make it active"), :url => admin_manage_user_state_change_path(user.id),:update => 'div_state_#{user.id}'  %>
            <%end%>
    </div>

controller side:

@user = User.find(params[:manage_user_id])
if @user.state=="active"
  @user.state = "pending"
 end
@user.save
render :update do |page|
  page.visual_effect :highlight, "div_state_#{@user.id}", :duration=>3
end


Well you are calling update but not actually updating anything.

I would pull this into a partial: _user_state.html.erb

<%= user.state  %>
<%if user.state == 'active'%>
  <%= link_to_remote image_tag("/images/de-active.png",:title =>" make it pending"), :url => admin_manage_user_state_change_path(user.id),:update => 'div_state_#{user.id}'  %>
<%else%>
  <%= link_to_remote image_tag("/images/active.png",:title =>" make it active"), :url => admin_manage_user_state_change_path(user.id),:update => 'div_state_#{user.id}'  %>
<%end%>

Then have your controller and your loop render the partial. The trick is to use :locals in the render call. render :partial => 'user_state', :locals => { :user => user }

So to put it all together:

Controller (since you have the update call in the link_to_remote it will take the return from the controller and replace the inner html of the passed id):

user = User.find(params[:manage_user_id])
if user.state=="active"
  user.state = "pending"
end
user.save
render :partial => 'user_state', :locals => { :user => user }

View:

<% for user in @users %>
  <% if !user.country.blank?%> <%= user.country.to_s.capitalize  %><%end%>
  <div id="div_state_<%=user.id%>">
  <%= render :partial => 'user_state', :locals => { :user => user } %>
  </div>
<% end %>

The only missing piece is the highlight. Look at the link_to_remote docs for more info. Specifically :success.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜