Ajax form with partial view update

Hi I am trying to use partials to update my view with re-rendering. I
created a form to submit using ajax

I am using slim-rails in place of erb

= simple_form_for(@landmark, remote: true) do |f|

  • if @landmark.errors.any?
    #error_explanation
    h2 = “#{pluralize(@landmark.errors.count, “error”)}
    prohibited this landmark from being saved:”
    ul
    • @landmark.errors.full_messages.each do |message|
      li = message
      = f.input :city
      = f.input :name
      = f.button :submit, class: ‘btn btn-primary btn-sm’

I created a js file create.js.erb which gets called on submit, inside
it I want to make use of a partial with jQuery to append the new item
as a new row in the table, like this (file create.js.erb):

$(‘tbody’).append("<%= j (render ‘landmark’, object: @landmark) %>");

My partial file ‘_landmark.html.slim’ looks like this

tr
td = landmark.city
td = landmark.name
td = link_to ‘Show’, landmark, class: ‘btn btn-info btn-xs’
td = link_to ‘Edit’, edit_landmark_path(landmark), class: ‘btn
btn-primary btn-xs’
td = link_to ‘Destroy’, landmark, :method => :delete, remote: true,
class: ‘delete-landmark btn btn-danger btn-xs’

This is the error I am getting, how can I pass data and make use of my
partial from javascript?

Started POST “/landmarks” for 127.0.0.1 at 2016-04-09 14:37:42 -0400
Processing by LandmarksController#create as JS
Parameters: {“utf8”=>“✓”, “landmark”=>{“city”=>“Toronto”,
“name”=>“Center Island”}, “commit”=>“Create Landmark”}
(0.2ms) begin transaction
SQL (0.2ms) INSERT INTO “landmarks” (“city”, “name”, “created_at”,
“updated_at”) VALUES (?, ?, ?, ?) [[“city”, “Toronto”], [“name”,
“Center Island”], [“created_at”, “2016-04-09 18:37:42.582440”],
[“updated_at”, “2016-04-09 18:37:42.582440”]]
(185.4ms) commit transaction
Rendered landmarks/_landmark.html.slim (39.3ms)
Rendered landmarks/create.js.erb (40.6ms)
Completed 500 Internal Server Error in 231ms (ActiveRecord: 185.8ms)

ActionView::Template::Error (undefined local variable or method
landmark' for #<#<Class:0x00563ca93d6b98>:0x007f7692eac7d8> Did you mean? @landmark): 1: tr 2: td = landmark.city 3: td = landmark.name 4: td = link_to 'Show', landmark, class: 'btn btn-info btn-xs' 5: td = link_to 'Edit', edit_landmark_path(landmark), class: 'btn btn-primary btn-xs' app/views/landmarks/_landmark.html.slim:2:in_app_views_landmarks__landmark_html_slim__1191694810663590152_70073623765620’
app/views/landmarks/create.js.erb:1:in
_app_views_landmarks_create_js_erb___2101680697775661015_70073623812280' app/controllers/landmarks_controller.rb:37:inblock (2 levels) in
create’
app/controllers/landmarks_controller.rb:29:in `create’

Rendered
/home/yadav/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
(0.5ms)
Rendered
/home/yadav/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(0.6ms)
Rendered
/home/yadav/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb
(12.3ms)


Kind Regards,
Rajinder Y.

It’s slowly starting to make sense, I changed up create.js.erb to

$(‘tbody’).append(“<%= j (render @landmark) %>”);

This seems to have fixed the error and I’m seeing the new item getting
added without a re-render.

On Sat, Apr 9, 2016 at 2:49 PM, Rajinder Y. [email protected]
wrote:

  ul

$(‘tbody’).append(“<%= j (render ‘landmark’, object: @landmark) %>”);
class: ‘delete-landmark btn btn-danger btn-xs’
“updated_at”) VALUES (?, ?, ?, ?) [[“city”, “Toronto”], [“name”,
1: tr
app/controllers/landmarks_controller.rb:29:in `create’


Kind Regards,
Rajinder Y.


Kind Regards,
Rajinder Y.