簡體   English   中英

創建后,Rails重定向到與kaminari分頁的記錄頁面

[英]Rails redirect to record page on pagination with kaminari after creation

在我的索引頁面之一中,我有kaminari分頁。 在同一頁面上,我還可以通過form_with處理create動作。 新記錄將按名稱保存在原位,因此可能處於分頁中間。 問題是-創建操作后可以直接重定向到此頁面嗎?

是的,您可以,只需在保存記錄后添加重定向,例如在此示例中,我們正在創建一個case_file,保存后,我們使用case_file的ID重定向到索引。

def create
  @case_file = CaseFile.new(case_file_params)
  if @case_file.save
    redirect_to case_files_path(to_record: @case_file.id), notice: "#{I18n.t 'created'}"
  else
    render :new
  end
end

然后在索引控制器上,我們找到已保存記錄的索引並計算頁面,如下所示

def index
  page = 1
  if (params[:page])
    page = params[:page]
  elsif (params[:to_record])
    index = CaseFile.order(:name).pluck(:id).index(params[:to_record])
    page = index/CaseFile.default_per_page + 1 # or if you don't have the default per model, just put the value or get the general default one
  end
  @case_files = CaseFile.all.order(:name).page(page)
end

暫無
暫無

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

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