Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 and Ajax interaction
    primarykey
    data
    text
    <p>I'm a beginner in ruby on rails and I was practicing Ajax but unfortunately I can't seem to get my ajax to work. I'm trying to Ajaxify my delete in the scaffold.</p> <p>Here's my controller:</p> <pre><code>class PlantsController &lt; 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 </code></pre> <p>And here's the view for it:</p> <pre><code>&lt;h1&gt;Listing plants&lt;/h1&gt; &lt;table class="show"&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Comment&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;%= render "plants/plant" %&gt; &lt;/table&gt; &lt;br /&gt; &lt;%= link_to 'New Plant', new_plant_path %&gt; </code></pre> <p>here's my partial:</p> <pre><code>&lt;% @plants.each do |plant| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= plant.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= plant.comment %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Show', plant %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Edit', edit_plant_path(plant) %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Destroy', plant, confirm: 'Are you sure?', :method =&gt; :delete, :remote =&gt; true %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre> <p>and lastly, here's the supposed destroy.js.erb that would refresh the DOM:</p> <pre><code>$('.show').html("&lt;%= render @plant %&gt;"); </code></pre> <p>Anyone can shine a light on this please?</p> <p>Additional Info:</p> <p>The thing is, when I click the <code>Destroy</code> link... The record is being deleted, but the DOM isn't rendering and I have to refresh to see the changes :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload