1

Ajax partial is not rendering in my rails application.

I am having an Article model. I am trying to add new articles using Ajax

Please find the code below.

cart.html.erb

<div id="articles">
  <%= render 'showarticles' %>
</div>

cart.js.erb

$('#articles').html('<%= escape_javascript(render('static_pages/showarticles')) %>');
$('.form-control').val('')

routes.rb

match 'cart/:id', to: 'static_pages#cart', via: [:get, :post]

_showarticles.html.erb

<%= form_for @article, :remote => true do |f| %>
<%= f.text_area :name, :class => "form-control" %>
<% end %>    
<% @art.each do |a| %>
<%= a.name %>
<% end %>

static_pages_controller.rb

def cart
  @art = Article.all
  @article = Article.new 
  respond_to do |format|
    format.html 
    format.js
  end
end

Thanks!

3
  • 1
    How are you triggering the call to the cart action? Commented Dec 21, 2014 at 22:03
  • @japed : This is how I triggered the cart action: redirect_to action: 'cart', controller: 'static_pages', id: cart.id Commented Dec 22, 2014 at 15:11
  • @japed: I am having a Cart controller. So after creating a Cart it redirects to the Cart page in the Create action of the controller. Commented Dec 22, 2014 at 16:25

2 Answers 2

1

I was able to fix the issue by adding this line to my routes.rb file:

match 'cart', to: 'static_pages#cart', via: [:get, :post]
Sign up to request clarification or add additional context in comments.

Comments

0

Ok,

so this

<%= form_for @article, :remote => true do |f| %>
<%= f.text_area :name, :class => "form-control" %>
<% end %>    
<% @art.each do |a| %>
<%= a.name %>
<% end %>

Will go to the articles create method. Which is why your cart.js.erb isn't called. Either set the url <%= form_for @article, :url => url_for(:controller => 'static_pages, :action => 'cart', :id => @cart.id), :remote => true do |f| %> or preferably handle it in the create action of your articles controller

Comments

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.