Py学习  »  Jquery

jQuery不在Rails视图中运行

Joan Buan • 4 年前 • 179 次点击  

在我的 views/contacts 我创建的文件夹 index.js.erb 归档并试图提取id contacts 从我的内容和 paginator 我的分页视图中的id。以下是 文件(甚至更改为 index.js 不起作用):

$('#contacts').html('<p>hello contacts</p>');
$('#paginator').html('<p>hello paginator</p>')

此代码将简单地获取联系人和 分页器

这是我想把上面的联系人id(在里面)拉上来的地方 views/contacts/index.html.erb :

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

我就在这里 id(在我的 views/layouts/_paginator.html.erb :

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

请注意 paginate 你上面看到的方法来自卡米纳里宝石。我把 remote:true 能够在场景后面进行javascript/jquery调用的代码。

我还注意到 不在我的编辑器上着色:

contacts_controller :

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]

    respond_to do |format|
      format.js
      format.html
    end 
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

索引.js 索引.js.erb views/contacts/index.js 文件。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54317
 
179 次点击  
文章 [ 1 ]  |  最新文章 4 年前