我试图让我可以创建一个Web应用程序的用户来创建它们已创建的对象的列表。在Ruby on Rails中创建对象列表或数组
例如,用户具有诸如杂货之类的对象的列表,其可以是从苹果到橘子到流行挞的任何东西。
然后,我会希望它能够返回用户添加到数据库中的所有杂货,并通过选择应该在其购物清单上的商品来创建列表。
最好这样做的风格,他们可以点击复选框,他们想要的,然后点击保存创建一个新的列表。
我查看了belongs_to,has_many关系,并试图创建一个列表对象,它有许多杂货,但我无法弄清楚这个策略的形式部分。我会很感激任何/所有的建议。谢谢!
这里是我目前,我本来忽略它,因为我不认为我是在正确的道路上的代码,但在这里它是无论如何以防万一/提供更多的背景:
杂货店型号:
class Item < ApplicationRecord
belongs_to :list, optional: true
end
列表模型
class List < ApplicationRecord
has_many :items
end
列表控制器
class ListsController < ApplicationController
before_action :authenticate_user!
layout 'backend'
def index
@lists = List.where(user_id: current_user.id)
end
def show
end
def new
@list = List.new
end
def edit
end
def create
@list = List.new(list_params)
@list.user = current_user
if @list.save
redirect_to list_path(@list.id), notice: 'List was successfully created.'
else
redirect_to list_path(@list.id), notice: 'List was not created.'
end
end
def update
respond_to do |format|
if @list.update(list_params)
format.html { redirect_to @list, notice: 'List was successfully updated.' }
format.json { render :show, status: :ok, location: @list }
else
format.html { render :edit }
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
end
def destroy
@list.destroy
respond_to do |format|
format.html { redirect_to lists_url, notice: 'List was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def list_params
params.require(:list).permit(:name, :items)
end
end
不知道办什么形式 - 试图沿着我将通过实现保持杂货和列表之间的关联第三个模型解决这个问题的http://apidock.com/rails/ActionView/Helpers/FormHelper/check_box
你现在的代码是什么样的? – raidfive
在你的问题中添加一些代码,你到底想要什么 –
确定 - 添加代码 – Kevin