【发布时间】:2015-09-18 15:45:58
【问题描述】:
我有一个带有数组列(“Product_list”)的模型,并且希望在用户选择数组元素时通过控制器删除/显示数组元素在视图中。
*Delete 会给出确认提示,并从同一视图中删除选定的数组元素。
*Show 将引导用户到另一个页面,其中将显示与用户选择的数组元素关联的另一个数据表中的产品详细信息)
我只是把??????????在 link_to 行中,因为我不知道可以为数组元素使用什么路径。我还从控制器中排除了 SHOW 和 DESTROY 操作,因为我只是不知道该放什么。
Index.html.erb
<% @product.product_list.each do |list| %>
<%= link_to(list) do %>
<div class="thumbnail">
<div class="caption">
<p><%= list %></p>
<%= link_to 'Show Details', ????????? %>
<%= link_to 'Destroy', ??????????, data: { confirm: 'Are you sure?' } %>
</div>
</div>
<% end %>
<% end %>
Products_controller.rb
def index
@product = Product.find(current_user.product)
rescue ActiveRecord::RecordNotFound
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
@product.save
Product.find_by_sql(["UPDATE products SET product_list = array_append(product_list, ?) WHERE user_id = ?", params[:product][:product_list], current_user]);
end
型号
create_table "products", force: :cascade do |t|
t.string "product_list", default: [], array: true
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "products", ["user_id"], name: "index_products_on_user_id", using: :btree
【问题讨论】:
标签: ruby-on-rails arrays postgresql