0

I'm trying to load the saved data using AJAX, to avoid refreshing the whole web page. I've been watching the rails cast Jquery Ajax video episode 136.

The problem occurs when I save the data on the rendered form. When I click save button nothing happens, but making a debug on chrome developer tool, I found the error ActionView::MissingTemplate in Locations#create. Locations is my model, and create the action to create.

I've Spent a lot of hours trying to find out what's wrong, and watching again and again the rail casts without any result. Please if someone could help.

This is the controller file locations_controller.rb

class LocationsController < ApplicationController

  def index
    @locations = Location.all
    @json = Location.all.to_gmaps4rails 
  end 

  def new
    @location = Location.new
  end

  def edit
    @location = Location.find(params[:id])
  end

  def create
    @location = Location.new(params[:location])
     respond_to do |format|
        format.html { redirect_to @location, notice: 'Location was successfully created.' }
        format.js   
    end
  end

def show
    @location = Location.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @location }
    end
  end
  def update
    @location = Location.find(params[:id])

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

  def destroy
    @location = Location.find(params[:id])
    @location.destroy

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

This is the model file location.rb

class Location < ActiveRecord::Base
  attr_accessible :address, :gmaps, :latitude, :longitude, :name
  validates_presence_of :name, :address

  acts_as_gmappable :check_process => false


  def gmaps4rails_address
    address
  end

   def gmaps4rails_infowindow  
  "<h1>#{self.name}</h1>"
  end

  def gmaps4rails_sidebar
   "<h4>#{name}</h4>" 
  end
end

The new.js.erb file

$('#new_link').hide().after('<%= j render("form") %>');

The create.js.erb file

$('#new_location').remove();
$('#new_link').show();
$('#allsites').empty().append('<%= j render(@location) %>'); 

The index.html.erb

<%= gmaps("markers" => {"data" => @json}) %>  


<table>
  <tr>
    <th>Name</th>
    <th>Address</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @locations.each do |location| %>
  <tr>
    <td><%= location.name %></td>
    <td><%= location.address %></td>
    <td><%= link_to 'Show', location %></td>
    <td><%= link_to 'Edit', edit_location_path(location) %></td>
    <td><%= link_to 'Destroy', location, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>
<div id="allsites">
//here I want to load all the data, ignore the table above
  </div>


<%= link_to 'New Location', new_location_path, id:"new_link", remote: true%>

<br />

Also the :remote => true has been added to the form

Partial _form.html.erb file

<%= form_for(@location, :remote => true) do |f| %>
  <% if @location.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@location.errors.count, "error") %> prohibited this location from being saved:</h2>

      <ul>
      <% @location.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :address %><br />
    <%= f.text_field :address %>
  </div>
  <div class="field">
    <%= f.label :latitude %><br />
    <%= f.text_field :latitude %>
  </div>
  <div class="field">
    <%= f.label :longitude %><br />
    <%= f.text_field :longitude %>
  </div>
  <div class="field">
    <%= f.label :gmaps %><br />
    <%= f.check_box :gmaps %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Working on Rails 3.2.13 This is my source http://railscasts.com/episodes/136-jquery-ajax-revised

Any help or sugestion will be apretiated, have a nice day everybody

EDIT: I'm sorry I forgot the screenshots HTML error 500 Crhome debug tool

Missing Template error

2
  • 1
    What exactly is your error? Commented Jul 10, 2013 at 13:16
  • 1
    Yeah, please attach a screenshot or paste a full backtrace of the error. Commented Jul 10, 2013 at 13:18

1 Answer 1

2

In your create action, you are not saving the object. All you are doing is making a call to new. You probably want to save it using @location.save after the call to new or use the create method instead:

@location = Location.create(params[:location])

Then your redirect_to @location goes to your show action which also needs a show.html.erb which you do not seem to have and that is probably the reason why you are getting the MissingTemplate error.

Oh, saw your screenshot, you don't have location partial defined as shown in your second screenshot. Since you're passing @location it's looking for _location.html.erb Please add that partial and use location to use your location's attributes instead of @location. Please report back if it still gives you problem.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi thanks for reply, this project was created using the scaffold generator all the realted files and views were created and used to work correctly. And yes I have the show.html.erb file too. Maybe the create.js.erb could be wrong?
Did you change the line @location = Location.new(params[:location]) to @location = Location.create(params[:location])? Since you are not actually saving it, the renderer is not able to find it. Please change that line and give it a try.
Yes I did, but still get the same error message "missing template...." did I tipe wrong this line? $('#allsites').empty().append('<%= j render(@location) %>');
Oh, saw your screenshot, you don't have location partial defined as shown in your second screenshot. Since you're passing @location it's looking for _location.html.erb. Please add that partial and use location to use your location's attributes instead of @location. Please report back if it still gives you problem.
Fixed ,Thanks for your Help, once I created the _location.html.erb , the new record refreshed immediately, now I have to figure it out how to display all the other records in the same view like a list. Maybe in another POST thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.