簡體   English   中英

redirect_to之后,Rails View不會刷新

[英]Rails View isn't refreshing after redirect_to

我正在使用一個示例Rails應用程序,該應用程序具有“用戶”,“事件”(由用戶創建),最終,“用戶”將能夠訂閱/參加這些事件。

我有一個表,當用戶單擊事件行中的link_to按鈕時,用戶可以訂閱和取消訂閱事件。 link_to從event_controller調用一個方法,該方法具有重定向到current_path的方法,但是當我單擊該按鈕時,視圖沒有任何反應,但是服務器似乎呈現了該視圖。

這是因為我是從event_controller調用它,而是從User使用show控制器嗎?

日志:

 Rendered users/show.html.erb within layouts/application (17.1ms)

用戶> show.html.erb

    <% if current_user?(@user) %>
        <td><%= link_to "Delete", e, method:  :delete,
                        data: { confirm: "You sure?" } %></td>
    <% else %>
        <% if attendingEvent?(current_user, e.id) %>
            <td><%= link_to "Not Going", events_remove_event_to_attend_path(event_id: e.id, event_owner_id:@user.id, current_path:request.fullpath), remote: true %></td>
        <% else %>
            <td><%= link_to "I Want To Go", events_add_event_to_attend_path(event_id: e.id, event_owner_id:@user.id, current_path:request.fullpath), remote: true %></td>
        <% end %>
    <% end %>

events_controller.rb

  def add_event_to_attend
    puts attend_params
    puts request.fullpath
    @attend = Attendee.new
    @attend.event_id = (attend_params[:event_id])
    @attend.user_id = current_user.id
    if @attend.save
      flash[:success] = "You created an assocations"
      redirect_to(attend_params[:current_path])
      puts 'saved'
    else
      flash[:danger] = "You did not create the event"
      puts 'not saved'
    end
  end

  def remove_event_to_attend
    @event_to_delete = Attendee.find_by_user_id_and_event_id(current_user.id,params[:event_id])
    @event_to_delete.destroy
    redirect_to(attend_params[:current_path])
  end

嗨,歡迎來到Stack Overflow。

所以,首先...我注意到你已經把一些put SES在你的代碼...你怎么在服務器日志/終端屏幕上看到了什么?

其次:您的rjs模板中有什么? 你有rjs模板嗎? 您已將鏈接設為遠程-這意味着您正在發送js請求,並且僅當您將其發送回告訴它的js時,瀏覽器才會更改其視圖。

另外,您可以通過從鏈接中刪除remote: true來快速修復此問題,然后重定向將工作正常,因為它不再是js請求,而只是普通請求。

我也是Ruby on Rails的新手。

我想在Taryn的答案中添加一些內容。 也許如果您使用redirect_back fallback_location: asd_path ,asd_path是同一頁面的路徑名(不適用於同一操作),則可以使用。

希望對您有所幫助,歡迎來到Rails! 我也是新手,但是rails很容易學習! 而我<3露比,一見鍾情。

快速說明 :我使用的是Rails 5,因此如果您使用的不是Rails 5,則可能無法使用。 簽入Rails文檔 (一定要檢查您使用的相同版本)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM