【问题标题】:NoMethodError at /todo_lists undefined method `save' for #<ActiveRecord::Associations::CollectionProxy []>NoMethodError at /todo_lists undefined method `save' for #<ActiveRecord::Associations::CollectionProxy []>
【发布时间】:2015-02-26 13:20:07
【问题描述】:

我正在制作一个带有待办事项列表的应用程序,并且我最近尝试添加从用户构建待办事项列表的功能。

我在更改以下行时遇到了问题。

def new
  @todo_list = current_user.todo_lists.build
end

def create
    @todo_list = current_user.todo_lists(todo_list_params)
    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
        format.json { render :show, status: :created, location: @todo_list }
      else
        format.html { render :new }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end

我正在使用以下代码。

def new
  @todo_list = TodoList.new
end

def create
    @todo_list = TodoList.new(todo_list_params)
    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
        format.json { render :show, status: :created, location: @todo_list }
      else
        format.html { render :new }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end

我通过创建新的迁移并迁移了数据库并为帖子创建了与用户的关联,从而为待办事项添加了一个用户 ID。不太确定出了什么问题,但任何帮助将不胜感激。 谢谢。

【问题讨论】:

    标签: ruby ruby-on-rails-4 activerecord


    【解决方案1】:

    改变这一行:

    @todo_list = current_user.todo_lists(todo_list_params)
    

    进入这个:

    @todo_list = current_user.todo_lists.build(todo_list_params)
    

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 2015-03-19
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      相关资源
      最近更新 更多