简体   繁体   中英

Ajax error for invalid arrguments on Ruby on Rails

I'm getting this error

ArgumentError in List#create

'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.

Here is my index.html.erb file

<div class="container">

    <div class="row">
        <div class="col-md-5"></div>
        <div class="col-md-4">
            <h4>Lists Homepage</h4>
        </div>
    </div>

    <div class="row">
    <div><%= link_to "New List", controller: "list", action: "new" , remote: true %>&nbsp;<%= image_tag("greenPlus.png", :alt => "add list", :height=>"15px", :width=>"15px") %></div>
    </div>

    <div class="row">&nbsp;</div>

    <div class="row">
        <div class="col-md-4">
            <div id="lists">
                <% @lists.each do |piece_list| %>
                    <%= render :partial=>"list/piece_list", :object=>piece_list %>
                <% end %>
            </div>
            <%= render "new" %>
        </div>
    </div>
</div>

This is create.js.erb

    $("#new_list").before("<div id='flash-notice'><%= escape_javascript(flash[:notice]) %></div>");
$("<%= escape_javascript(render @piece_list) %>").appendTo("#lists");
$("#new_list")[0].reset();

This is piece_list.html.erb

<table class="table table-bordered"> 
    <thead> 
        <tr> 
            <th><%= piece_list.name %></th>
            <th><%= image_tag("redCross1.png", :alt => "delete list", :class => "deleteList", :height=>"15px", :width=>"15px") %></th>
        </tr> 
    </thead> 
    <tbody class="list-index">
        <% if piece_list.items.any? %>
            <% piece_list.items.each do |item| %>
                <%= render :partial=>"list/item", :object=>item %>
            <% end %>
        <% end %>
    </tbody> 
</table>

and this the controller

    class ListController < ApplicationController

    def index
        @list = List.new    
        @lists = List.joins("LEFT JOIN  `items` ON `items`.`list_id` = `lists`.`id`")
    end

    def create

        @piece_list = List.create(list_params)

        if @piece_list.save
            flash[:notice] = "Thank you for submitting"
            respond_to do |format|
                format.html { redirect_to @lists }
                format.js {}
            end
        else
            format.html { render action: "new" }
            format.js {}
        end
    end

    private
    def list_params
        params.require(:list).permit(:name)
    end

end

It looks like @piece_list is not defined in create.js.erb - you were probably supposed to use @list instead. Please try to replace and see if it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM