简体   繁体   English

link_to远程:真正重定向到HTML

[英]link_to remote: true redirecting to HTML

At the /tags page I have a link with remote: true . /tags页面,我有一个remote: true链接remote: true It should be a link to an ajax request. 它应该是ajax请求的链接。 But there are two requests, as JS and as HTML. 但是有两个请求,即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:

If I provide a new.html.erb , this MissingTemplate error is over, but the page is redirected to new.html. 如果我提供了new.html.erb ,则此MissingTemplate错误已结束,但是页面被重定向到new.html。 What could be wrong with that request or that link? 该请求或链接可能有什么问题?

Edit The controller code class TagsController < ApplicationController before_action :set_tag, only: [:show, :edit, :update, :destroy, :fix_correct_name] before_action :set_tags, only: [:new, :edit] 编辑控制器代码类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

In your new action, you'll need a response_to to handle the ajax call 在您的新操作中,您需要一个response_to来处理ajax调用

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

Also, you'll need a new.js.erb file and a _new.html.erb partial to handle the response and update the view. 另外,您将需要一个new.js.erb文件和一个_new.html.erb局部文件来处理响应并更新视图。

Inside your new.js.erb, you will have to render the view with something like this 在您的new.js.erb内部,您将不得不使用如下方式渲染视图

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

And your new partial will simply hold your form (or whatever is it you wanna render) 而您的新部分将仅保留您的表单(或您想要呈现的内容)

我做到了现在的工作,只更换require jquery_ujs通过require rails-ujsapplication.js ,加入轨-UJS宝石。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM