So I started to learn ajax on ruby on rails and very ashame to ask some error, but this error very annoying me, I don't get it, it's because my OS (windows 10) or my code
So I have 5 files, actually I just edit the code after generate from scaffold
The error always error internal server (500), but I couldn't solved it for 2 days, and the data still inserted to database but the div#data-list wont refresh
Here's the code :
-
my index.html.erb :
Listing ajaxxxes
<table> <thead> <tr> <th>Title</th> <th>Description</th> <th colspan="3"></th> </tr> </thead> <div id="data-list"> <%= render 'data' %> </div> </table> <br> <%= render 'form' %> <%= link_to 'New Ajaxxx', new_ajaxxx_path %>
-
my _data.html.erb :
<tbody> <% @ajaxxxes.each do |ajaxxx| %> <tr> <td><%= ajaxxx.title %></td> <td><%= ajaxxx.description %></td> <td><%= link_to 'Show', ajaxxx %></td> <td><%= link_to 'Edit', edit_ajaxxx_path(ajaxxx) %></td> <td><%= link_to 'Destroy', ajaxxx, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody>
-
my _form.html.erb:
<%= form_for(@ajaxxx, remote: true) do |f| %> <% if @ajaxxx.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@ajaxxx.errors.count, "error") %> prohibited this ajaxxx from being saved:</h2> <ul> <% @ajaxxx.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :title %><br> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :description %><br> <%= f.text_area :description %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
-
my ajaxxxes_controller :
class AjaxxxesController < ApplicationController before_action :set_ajaxxx, only: [:show, :edit, :update, :destroy] # GET /ajaxxxes # GET /ajaxxxes.json def index @ajaxxxes = Ajaxxx.all @ajaxxx = Ajaxxx.new end # GET /ajaxxxes/new def new @ajaxxx = Ajaxxx.new end # POST /ajaxxxes # POST /ajaxxxes.json def create @ajaxxx = Ajaxxx.new(ajaxxx_params) respond_to do |format| if @ajaxxx.save format.html { redirect_to action: "index", notice: 'Ajaxxx was successfully created.' } format.json { render :show, status: :created, location: @ajaxxx } format.js else format.html { render :new } format.json { render json: @ajaxxx.errors, status: :unprocessable_entity } end end end private # Use callbacks to share common setup or constraints between actions. def set_ajaxxx @ajaxxx = Ajaxxx.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def ajaxxx_params params.require(:ajaxxx).permit(:title, :description) end end
-
and lastly my create.js.erb :
<% if @ajaxxx.title %> console.log("Ajaxxx created!"); $("#data-list").html("<%= escape_javascript(render 'data') %>"); <% else %> console.log("Failed"); <% end %>
And here's the log:
Started GET "/" for 127.0.0.1 at 2015-11-01 17:06:01 +0700
[1m[36mActiveRecord::SchemaMigration Load (0.0ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
Processing by AjaxxxesController#index as HTML
[1m[35mAjaxxx Load (0.0ms)[0m SELECT "ajaxxxes".* FROM "ajaxxxes"
Rendered ajaxxxes/_data.html.erb (24.0ms)
Rendered ajaxxxes/_form.html.erb (60.0ms)
Rendered ajaxxxes/index.html.erb within layouts/application (160.0ms)
Completed 200 OK in 468ms (Views: 438.2ms | ActiveRecord: 0.0ms)
Started GET "/assets/searches.css?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started GET "/assets/ajaxxxes.js?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started GET "/assets/indices.js?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started GET "/assets/items.js?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started GET "/assets/searches.js?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2015-11-01 17:06:02 +0700
Started POST "/ajaxxxes" for 127.0.0.1 at 2015-11-01 17:06:19 +0700
Processing by AjaxxxesController#create as JS
Parameters: {"utf8"=>"✓", "ajaxxx"=>{"title"=>"Test-51", "description"=>"Test-51"}, "commit"=>"Create Ajaxxx"}
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mSQL (0.0ms)[0m INSERT INTO "ajaxxxes" ("created_at", "description", "title", "updated_at") VALUES (?, ?, ?, ?) [["created_at", "2015-11-01 10:06:19.915267"], ["description", "Test-51"], ["title", "Test-51"], ["updated_at", "2015-11-01 10:06:19.915267"]]
[1m[36m (15.6ms)[0m [1mcommit transaction[0m
Rendered ajaxxxes/_data.html.erb (62.5ms)
Rendered ajaxxxes/create.js.erb (109.4ms)
Completed 500 Internal Server Error in 344ms
ActionView::Template::Error (undefined method `each' for nil:NilClass):
1: <tbody>
2: <% @ajaxxxes.each do |ajaxxx| %>
3: <tr>
4: <td><%= ajaxxx.title %></td>
5: <td><%= ajaxxx.description %></td>
app/views/ajaxxxes/_data.html.erb:2:in `_app_views_ajaxxxes__data_html_erb___834628740_45308136'
app/views/ajaxxxes/create.js.erb:3:in `_app_views_ajaxxxes_create_js_erb___336360114_45416052'
app/controllers/ajaxxxes_controller.rb:30:in `create'
Rendered C:/Ruby22/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/Ruby22/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.0ms)
Rendered C:/Ruby22/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (665.1ms)