开发者

redirect_to isn't refreshing page in Rails 3

开发者 https://www.devze.com 2023-02-22 07:44 出处:网络
I have a Dashboard that contains several bits of information. One of these bits is Events of which the user wants to be reminded. Each of these Events are displayed along w/ an empty checkbox. If the

I have a Dashboard that contains several bits of information. One of these bits is Events of which the user wants to be reminded. Each of these Events are displayed along w/ an empty checkbox. If the user checks the checkbox, the Event record is updated and the Dashboard should refresh without the recently checked Event.

All this works except for the last part. What makes this so puzzling to me is that a look into the log shows identical logging statements for this redirect to the Dashboard after the update of the Event object as for the original loading of the Dashboard. The query to find which Event objects to display on the Dashboard is run in both cases according to the logs. It's as if the actual update of the Event object doesn't occur quick enough for the subsequent query, and that subsequent query 'finds' the original un-updated data...

Here's the code:

View:

<% @triggered_events.each do |event| %>
      <p>
        <%= image_tag 'indicator.gif', :id => "indicator-#{event.id}",:style => 'display: none' %>
        <span class="grayItalics"><%= truncate(event.title, :length => 25) %></span> --- <% if event.contact_id != nil %>
        <%= full_name_by_contact(Contact.find(event.contact_id)) %> <% end %>
        <%= l event.start, :format => :yyyyMMdd %> (<%= event.reminder_lead_value %> <%= event.reminder_lead_unit %>)
        <%= check_box_tag 'event[recognized_flag]', "1", event.recognized_flag, :onclick => toggle_value(event) %>
        <%= link_to 'View Event', event %>
       <hr>
      </p>
    <% end 开发者_如何学运维%>

Helper:

def toggle_value(object)
  remote_function(:url => dashboard_path(:id => object),
    :method   => :put,
    :before   => "Element.show('indicator-#{object.id}')",
    :complete => "Element.hide('indicator-#{object.id}')" )
end

Controller:

def update

  @event = Event.find(params[:id])

  @event.recognized_flag=true

  respond_to do |format|
    if @event.update_attributes(params[:event])
      format.html { redirect_to dashboard_path }
      format.xml  { head :ok }
    else
      format.html { redirect_to dashboard_path }
      format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
    end
  end

end

EDIT: adding routes:

get 'dashboard' => 'dashboard#index'
put 'dashboard' => 'dashboard#update' # added specifically for the Event update mentioned in this question

Now, this update on an Event object is occuring in the Dashboard controller... which probably isn't the greatest. However the Event controller has it's own 'Update' method in use and redirects to a path different than the Dashboard, so that was not available for this instance... but I don't think it's the cause of this problem.

Any help is greatly appreciated.

I'd also add that a refresh of the page (F5) shows that the Event object is being updated (and hence is not displayed on the Dashboard after the F5).


Should you have

format.html { redirect_to dashboards_path }

or

format.html { redirect_to dashboard_path(@event) }
0

精彩评论

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

关注公众号