I wanted to use create action with ajax on 'index' page. I found some similiar problems here on SO, tried to use it, but no help for me so far. HTML works fine, only JS is a problem.
walls_controller:
def items
@item = Item.new
@items = current_user.items
find_items
@ads = @items_ads
end
def create
@items = current_user.items
find_items
#@items_ads via find_items method
@ads = @items_ads
@item = current_user.items.build item_params
current_user.save
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
items.html.erb:
<div id="items1">
<div class="row">
<div class="col-md-3">
<h3>Wall of user <%= current_user.name %></h3>
<div>
<%= render 'item_form', item: @item %>
</div>
Currently you are looking for these items:
<div>
<%= render 'items_list', items: @items %>
</div>
</div>
<div>
<%= render 'ads/ads_list', ads: @ads %>
</div>
</div>
</div>
_item_form.html.erb:
<%= form_for(item, url: wall_items_path, remote: true) do |f| %>
...
First I had error:
ActionView::Template::Error (undefined local variable or method `items' for #<#<Class:...
then I changed create.js.erb from
$("#items1").html("<%= render 'items_list', locals: {items: @items} %>");
$("#items1").html("<%= render 'ads_list', locals: {ads: @ads} %> ");
to
$("#items1").html("<%= render 'items_list', items: @items %>");
$("#items1").html("<%= render 'ads/ads_list', ads: @ads %>");
and now it doesn't show me any error, but no visible change when trying JS on browser. Thanks for help.
$("#items1").html("<%= render 'items_list', locals: {items: @items} %>");
, just add thej
$("#items1").html("<%= j render('items_list', locals: {items: @items}) %>");
– fanta Nov 3 '16 at 22:22