首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 3与Ajax交互

Rails 3与Ajax交互
EN

Stack Overflow用户
提问于 2013-12-23 20:03:20
回答 2查看 122关注 0票数 0

我是rails的初学者,我正在练习Ajax,但不幸的是,我似乎无法让ajax发挥作用。我试着在脚手架上把我的删除写成Ajaxify。

这是我的控制器:

代码语言:javascript
复制
class PlantsController < ApplicationController
  # GET /plants
  # GET /plants.json
  def index
    @plants = Plant.all

    respond_to do |format|
      format.html #index.html.erb
      format.json { render json: @plants }
    end
  end

  # GET /plants/1
  # GET /plants/1.json
  def show
    @plant = Plant.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @plant }
    end
  end

  # GET /plants/new
  # GET /plants/new.json
  def new
    @plant = Plant.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @plant }
    end
  end

  # GET /plants/1/edit
  def edit
    @plant = Plant.find(params[:id])
  end

  # POST /plants
  # POST /plants.json
  def create
    @plant = Plant.new(params[:plant])

    respond_to do |format|
      if @plant.save
        format.html { redirect_to @plant, notice: 'Plant was successfully created.' }
        format.json { render json: @plant, status: :created, location: @plant }
      else
        format.html { render action: "new" }
        format.json { render json: @plant.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /plants/1
  # PUT /plants/1.json
  def update
    @plant = Plant.find(params[:id])

    respond_to do |format|
      if @plant.update_attributes(params[:plant])
        format.html { redirect_to @plant, notice: 'Plant was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @plant.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /plants/1
  # DELETE /plants/1.json
  def destroy
    # binding.pry
    @plant = Plant.find(params[:id])
    @plant.destroy

    respond_to do |format|
      format.html { redirect_to plants_url }
      format.js
      format.json { head :no_content }
    end
  end
end

这里是它的视野:

代码语言:javascript
复制
<h1>Listing plants</h1>

<table class="show">
  <tr>
    <th>Name</th>
    <th>Comment</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<%= render "plants/plant" %>

</table>

<br />

<%= link_to 'New Plant', new_plant_path %>

这是我的部分:

代码语言:javascript
复制
<% @plants.each do |plant| %>
  <tr>
    <td><%= plant.name %></td>
    <td><%= plant.comment %></td>
    <td><%= link_to 'Show', plant %></td>
    <td><%= link_to 'Edit', edit_plant_path(plant) %></td>
    <td><%= link_to 'Destroy', plant, confirm: 'Are you sure?', :method => :delete, :remote => true %></td>
  </tr>
<% end %>

最后,下面是应该刷新DOM的destroy.js.erb:

代码语言:javascript
复制
$('.show').html("<%= render @plant %>");

有人能照一下这个吗?

附加信息:

问题是,当我点击Destroy链接.该记录正在被删除,但DOM没有呈现,我必须刷新以查看更改:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-24 00:44:56

经过一夜艰苦的思考和吃,这是我已经制定的解决方案。

首先,我将描述我的问题的根源。这段代码rails似乎试图呈现路由‘/ $('.show').html("<%= render @plant %>") /:id/’,但不幸的是,您刚刚删除了它。我所做的工作是将@plants = Plant.all添加到@plant.destroy代码下面,在destroy.js.erb中,我呈现了部分内容,如下所示:

代码语言:javascript
复制
$('.show').html("<%= render 'plants/plant' %>");

使用该代码,它将使用新的一组查询值呈现分部。希望我解释得对。

票数 1
EN

Stack Overflow用户

发布于 2013-12-23 21:48:57

经过多次试验,我尝试了一个新的教程,这对我来说很好。教程的链接在底部。如果我找到这个问题的解决方案,我还会更新这篇文章。

教程链接--> 链接在这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20750372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档