【问题标题】:Rails jQuery is rendering text instead of collectionRails jQuery 渲染文本而不是集合
【发布时间】:2020-04-18 20:23:13
【问题描述】:

我在我的 javascript 文件夹“load-contacts.js”中创建了一个文件,希望尝试通过 jquery 加载集合和分页。

$(document).on('turbolinks:load', function() {
    $('#contacts').html("<%= j render(@contacts) %>");
    $('#paginator').html("<%= j paginate @contacts, remote: true %>");
  });

当我运行此代码时,它实际上可以工作并更改它打算更改的部分。但是,它只是将其呈现为文本,而不是集合和分页(kaminari gem)。

它是这样的:

知道我在这里缺少什么吗?

更新:

这是我的联系人控制器

class ContactsController < ApplicationController

  before_action :find_params, only: [:edit, :update, :destroy]

  def index

    # catch the group id from params in session
    session[:selected_group_id] = params[:group_id]

    @contacts = Contact.by_group(params[:group_id]).search(params[:term]).order(created_at: :desc).page params[:page]


end 

def autocomplete
  @contacts = Contact.search(params[:term]).order(created_at: :desc).page(params[:page])
  render json: @contacts.map { |contact| { id: contact.id, value: contact.name } }
end

def new
  @contact = Contact.new
end 

def create
  @contact = Contact.new(contact_params)
  if @contact.save
    flash[:success] = "Contact was successfully created."
    redirect_to(previous_query_string)
  else
    render 'new' 
  end
end 

def edit
end 

def update
  if @contact.update(contact_params)
    flash[:success] = "Contact successfully updated."
    redirect_to(previous_query_string)
  else
    render 'edit'
  end
end 

def destroy
  @contact.destroy
  flash[:success] = "Contact successfuly deleted."
  redirect_to contacts_path
end 

private

def contact_params
   params.require(:contact).permit(:name, :email, :phone, :mobile, :company, :address, :city, :state, :country, :zip, :group_id, :avatar)
end 

def find_params
  @contact = Contact.find(params[:id])
end

def previous_query_string
  session[:selected_group_id] ? { group_id: session[:selected_group_id] } : {} 
end 


end

这是 kaminari gem 分页的部分:

 <div class="card-footer">
                <div class="pagination justify-content-center" id="paginator">
              <%= paginate @contacts, remote: true %>
           </div>
          </div>

这是我想在views/contacts/index.html.erb中渲染联系人的地方:

 <table class="table" id="contacts">
                 <%= render partial: "contact", object: @contacts, remote: true %>            
              </table>    

【问题讨论】:

  • 你对渲染后端和前端的理解太难了。即使这个 JS 部分有效,分页链接也无法按照您的期望更改联系人列表。
  • 那我该怎么做呢?这样做的正确方法是什么?
  • 你能分享你的联系人控制器吗? (返回@contacts 和分页@contacts 的后端部分)
  • 还有这个页面的html文件
  • Thang:我更新了上面的问题。

标签: javascript jquery ruby-on-rails ruby


【解决方案1】:

您不能在 .erb 之外使用 j render。因为j render是rails的方法,你的js只知道它是字符串

你需要的是在你的 html.erb 中使用 script 标签,或者使用 rails ajax。它按预期工作

参考:How to render partial on the same page after clicking on link_to with AJAX

【讨论】:

    【解决方案2】:

    在调用 render 之前您是否尝试过 escape_javascript。

    类似:

    <%= escape_javascript render(my_partial) %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 2011-11-20
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多