2017-01-10 56 views
4

我试图让我可以创建一个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

+0

你现在的代码是什么样的? – raidfive

+0

在你的问题中添加一些代码,你到底想要什么 –

+0

确定 - 添加代码 – Kevin

回答

2

线的东西。然后你可以通过使用:accepts_nested_attributes_for来处理它。

举个例子,这里是我会怎么构建模型:

class List < ApplicationRecord 
    has_many :list_items, inverse_of: :list 
    has_many :items,  through: :list_items 

    # This allows ListItems to be created at the same time as the List, 
    # but will only create it if the :item_id attribute is present 
    accepts_nested_attributes_for :list_items, reject_if: proc { |attr| attr[:item_id].blank? } 
end 

class Item < ApplicationRecord 
    has_many :list_items 
    has_many :lists,  through: :list_items 
end 

class ListItem < ApplicationRecord 
    belongs_to :list, inverse_of: :list_items 
    belongs_to :item 
end 

有了这个模型的结构,这里是创建一个新的列表视图的一个例子。

<h1>New List</h1> 
<%= form_for @list do |f| %> 
    <% @items.each_with_index do |item, i| %> 
    <%= f.fields_for :list_items, ListItem.new, child_index: i do |list_item_form| %> 
     <p> 
     <%= list_item_form.check_box :item_id, {}, item.id, "" %> <%= item.name %> 
     </p> 
    <% end %> 
    <% end %> 
    <p> 
    <%= f.submit 'Create List' %> 
    </p> 
<% end %> 

为了解释这里发生了什么,@items是预装的变量有所有可以添加到列表中的项目。我遍历每个项目,然后手动将其传递给FormBuilder方法fields_for

因为我手动做到这一点,我有在同一时间到指定:child_index,否则每个复选框将得到同样的name属性(即name="list[list_item_attributes][0][item_id]")为上一个项目,并提交给当他们将覆盖每个人的价值服务器。

而且check_box具有以下声明FormBuilder方法:

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") 

所以在上面的表格,我替换这些默认值,这样,当被选中复选框,它已经从item.id,如果它的价值未检查,值为空白。将这与在List模型中的accepts_nested_attributes_for的声明结合起来,在那里我们说如果:item_id是空的,它应该被拒绝,并且我们得到仅为被检查的项目创建ListItems的结果。

的最后一件事,使这项工作,是允许嵌套属性在控制器中,像这样:

def allowed_params 
    params.require(:list).permit(list_items_attributes: [:item_id]) 
end 
+0

请看看我做了什么 - 这对我的目的来说似乎相当复杂。 – Kevin

+0

它看起来不错,只要它工作:)我展示的方法可能有点复杂,是的,也许更适合,当你想添加更多的功能,如你想添加多少个项目和多少次你已经添加了一个项目到不同的列表中,但是如果你不想要那个,那么你的方法很好。作为最后一点,解决问题的另一种解决方法是使用list关联来创建Item,即@item = @ list.items.create(allowed_pa​​rams)'。这将默认分配list_id – DanneManne

0

于是我发现了以下问题,并用它,以便能够完成我通缉。

Display a checkbox list instead of multiple select

我简单地实现以下

<% form_for @list do |f| %> 
    <% Item.all.each do |item| 
    <%= f.check_box(:items, { :multiple => true }, item.id) %> 
    <% end %> 
<% end %> 

在这一点我有没有分配LIST_ID错误。我进行了一次迁移,以便将attirbute“list_id”添加到Item模型,并且这固定了它,并且按照我的意愿工作。

有没有问题?

相关问题