簡體   English   中英

link_to遠程:真正重定向到HTML

[英]link_to remote: true redirecting to HTML

/tags頁面,我有一個remote: true鏈接remote: true 它應該是ajax請求的鏈接。 但是有兩個請求,即JS和HTML。

<%= link_to 'New', new_tag_path, class: "btn btn-outline btn-primary", remote: true %>

INFO -- : Started GET "/tags/new" for 192.168.18.236 at 2018-06-13 11:44:18 -0300
INFO -- : Processing by TagsController#new as JS
INFO -- :   Rendered tags/_form.html.erb (41.0ms)
INFO -- :   Rendered tags/_modal.html.erb (41.5ms)
INFO -- :   Rendered tags/new.js.erb (49.2ms)
INFO -- : Completed 200 OK in 63ms (Views: 50.3ms | ActiveRecord: 2.2ms)
INFO -- : Started GET "/tags/new" for 192.168.18.236 at 2018-06-13 11:44:18 -0300
INFO -- : Processing by TagsController#new as HTML
INFO -- : Completed 500 Internal Server Error in 14ms (ActiveRecord: 1.9ms)
FATAL -- : 
ActionView::MissingTemplate (Missing template tags/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:

如果我提供了new.html.erb ,則此MissingTemplate錯誤已結束,但是頁面被重定向到new.html。 該請求或鏈接可能有什么問題?

編輯控制器代碼類TagsController <ApplicationController before_action:set_tag,僅:[:show,:edit,:update,:destroy,:fix_correct_name] before_action:set_tags,僅:[:new,:edit]

  # GET /tags/new
  def new
    @tag = Tag.new
  end

  # GET /tags/1/edit
  def edit
  end

  # POST /tags
  def create
    @tag = Tag.new(tag_params)

    respond_to do |format|
      if @tag.save
        format.html { redirect_to action: "index", notice: 'Tag adicionada.' }
      else
        format.html { render :new }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_tag
      @tag = Tag.find(params[:id])
    end

    def set_tags
      @tags = Tag.all.pluck(:name, :id)
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def tag_params
      params.fetch(:tag, {}).permit(:name, :parent_id, :tag_name, :parent_name, :no_parent)
    end
end

在您的新操作中,您需要一個response_to來處理ajax調用

def new
  @tag = Tag.new
  respond_to { |format| format.js }
end 

另外,您將需要一個new.js.erb文件和一個_new.html.erb局部文件來處理響應並更新視圖。

在您的new.js.erb內部,您將不得不使用如下方式渲染視圖

$(“div-id-name”).html(“<%= j render partial: “new” %>”) 

而您的新部分將僅保留您的表單(或您想要呈現的內容)

我做到了現在的工作,只更換require jquery_ujs通過require rails-ujsapplication.js ,加入軌-UJS寶石。

暫無
暫無

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

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