ruby on rails - Modal updating data but throwing error -


i want able update data in modal, , have updated data displayed in table on close of modal.

it updating db, after throws error:

nomethoderror in products#update 

and highlights line of code: , says @products nil

<% @products.each |product| %> 

this in index.html.erb

<div class="container-fluid col-md-8"> <h1>products</h1> <table class="table table-striped table-hover table-bordered table-condensed sortable"> <thead>   <tr>     <th><a>title</a></th>     <th><a>asin</a></th>     <th><a>price</a></th>     <th> </th>     <th><a>toggle</a></th>   </tr> </thead> <tbody>   <% @products.each |product| %>     <tr>       <td><%= product.title %></td>       <td><%= link_to product.asin, "" %></td>       <td><%= number_to_currency(product.price) %></td>       <td>         <%= link_to 'edit', "" , :class => 'btn btn-primary btn-sm' %>         <%= link_to 'delete', "" , :method => :delete, :confirm => 'are          sure?', :class => 'orange btn btn-primary btn-sm' %>       </td>       <td>         <a href="#" class="btn btn-primary btn-sm" data-toggle="modal"          data-remote="<%= edit_product_path(product) %>" #largemodal<%=          product.asin %> data-target="#largemodal<%= product.asin%>"         >click</a>       </td>           <div class="modal fade" id="largemodal<%= product.asin %>"          tabindex="-1" role="dialog" aria-labelledby="largemodal"          aria-hidden="true">           <div class="modal-dialog modal-lg">             <div class="modal-content">               <div class="modal-header">                 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>                 <h4 class="modal-title" id="mymodallabel">edit product: <%= product.asin %></h4>               </div>               <div class="modal-body">                 <h3>modal body</h3>                   <div class="well col-md-8 col-md-offset-2">                     <%= form_for product |f| %>                      <label>title</label>                     <%= f.text_field :title %>                      <label>asin</label>                     <%= f.text_field :asin %>                      <%= f.submit "submit", class: "btn btn-primary btn-sm" %>                      <% end %>                   </div>               </div>               <div class="modal-footer">                 <button type="button" class="btn btn-default"                 data-dismiss="modal">close</button>                 <button type="button" class="btn btn-primary">update</button>               </div>             </div>           </div>         </div>     </tr>   <% end %> </tbody> 

and here homecontroller

class homecontroller < authenticatedcontroller  def index   @products = product.paginate(page: params[:page], per_page: 4)   @home = @products end  end 

and productscontroller:

class productscontroller < applicationcontroller  def new     @product = product.new end  def edit     @product = product.find(params[:id]) end  def update     @product = product.find(params[:id])      if @product.update(product_params)         flash[:success] = "updated succesfully!"         render 'home/index'      else       render 'shared/modal' #this same modal partial..     end  end      private     def product_params        params.require(:product).permit(:title, :asin)     end  end 

this happening because when renders modal, @products nil. need define or @products.each can't work.

could coming fact don't define params[:page] when render home/index.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -