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.
Here's my controller:
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
And here's the view for it:
<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 %>
here's my partial:
<% @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 %>
and lastly, here's the supposed destroy.js.erb that would refresh the DOM:
$('.show').html("<%= render @plant %>");
Anyone can shine a light on this please?
Additional Info:
The thing is, when I click the Destroy
link... The record is being deleted, but the DOM isn't rendering and I have to refresh to see the changes :(
Upon multiple trials I have tried a new tutorial which works fine for me. The link of the tutorial is at the bottom. I will still update this post if ever I found out the solution of this question.
tutorial link ---> Link here
After a grueling night of thinking and eating here's the solution I have formulated.
First I will describe the root of my problem. It seems that this piece of code $('.show').html("<%= render @plant %>")
, rails is trying to render route '/plants/:id/' but unfortunately, you just deleted it. The work around I made is to add @plants = Plant.all
below the @plant.destroy
code and in the destroy.js.erb
i rendered the partial and it goes something like this:
$('.show').html("<%= render 'plants/plant' %>");
with that code, it will render the partial with the new set of queries values. Hopefully I explained it right.