Ruby on rails 在rails中创建联接表后,类别停止保存
这是我第一次创建联接表,我有点卡住了。我有两张表:类别和产品。我最初将其设置为产品在类别上所属的位置,类别中有许多产品。我刚刚创建了一个联接表,并将我的关联更改为has_和_beliens_to _many 我有一个表单,可以为一个产品选择多个类别,但所选类别不再保存在数据库中 在我的控制器中,我在我的产品参数中为category_id创建了一个数组,我想知道我的数据库中是否也有需要更新的内容?我应该更改数据库中的类别id吗 这是我的桌子:Ruby on rails 在rails中创建联接表后,类别停止保存,ruby-on-rails,associations,jointable,Ruby On Rails,Associations,Jointable,这是我第一次创建联接表,我有点卡住了。我有两张表:类别和产品。我最初将其设置为产品在类别上所属的位置,类别中有许多产品。我刚刚创建了一个联接表,并将我的关联更改为has_和_beliens_to _many 我有一个表单,可以为一个产品选择多个类别,但所选类别不再保存在数据库中 在我的控制器中,我在我的产品参数中为category_id创建了一个数组,我想知道我的数据库中是否也有需要更新的内容?我应该更改数据库中的类别id吗 这是我的桌子: create_table "categories_pr
create_table "categories_products", id: false, force: :cascade do |t|
t.integer "category_id", null: false
t.integer "product_id", null: false
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "product_id"
t.integer "category_id"
end
create_table "products", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "image_url"
t.integer "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image", default: "{}"
t.integer "category_id"
t.integer "product_id"
end
以下是我的模型关联:
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories, :join_table => :categories_products
class Category < ActiveRecord::Base
has_and_belongs_to_many :products, :join_table => :categories_products
类产品:类别\u产品
类类别:categories_products
我的新产品表单中的类别选择器:
<div class="field">
<%= f.label :category_ids %><br>
<%= f.select :category_ids, Category.all.collect {|x| [x.name, x.id]}, {}, :multiple => true %>
</div>
正确%>
产品控制员:
class ProductsController < ApplicationController
before_filter :authenticate_admin!, :except => [:index, :show, :earings]
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
@products = Product.all
respond_to do |format|
format.html # index.html.erb
format.js # index.js.erb
format.json { render json: @products }
end
end
def show
end
def new
@products = Product.new
@categories = Category.order(:name)
end
# GET /products/1/edit
def edit
@categories = Category.order(:name)
end
# POST /products
# POST /products.json
def create
@products = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :description, :image, :price, :category_ids => [])
end
def search_params
default_params = {}
default_params.merge({user_id_eq: current_user.id}) if signed_in?
# more logic here
params[:q].merge(default_params)
end
end
class ProductsController[:索引,:显示,:耳]
在\操作之前:设置\产品,仅:[:显示,:编辑,:更新,:销毁]
#获取/获取产品
#GET/products.json
def索引
@products=Product.all
回应待办事项|格式|
format.html#index.html.erb
format.js#index.js.erb
format.json{render json:@products}
结束
结束
def秀
结束
def新
@products=Product.new
@类别=类别。顺序(:名称)
结束
#获取/产品/1/编辑
定义编辑
@类别=类别。顺序(:名称)
结束
#邮政/产品
#POST/products.json
def创建
@产品=产品。新(产品参数)
回应待办事项|格式|
如果@product.save
format.html{将_重定向到@product,注意:“产品已成功创建”。}
format.json{render:show,status::created,location:@product}
其他的
format.html{render:new}
format.json{render json:@product.errors,status::unprocessable_entity}
结束
结束
结束
#补丁/放置/产品/1
#PATCH/PUT/products/1.json
def更新
回应待办事项|格式|
if@product.update(产品参数)
format.html{将_重定向到@product,注意:'产品已成功更新。}
format.json{render:show,status::ok,location:@product}
其他的
format.html{render:edit}
format.json{render json:@product.errors,status::unprocessable_entity}
结束
结束
结束
#删除/products/1
#删除/products/1.json
def销毁
@产品销毁
回应待办事项|格式|
format.html{重定向到产品的url,注意:“产品已成功销毁”。}
format.json{head:no_content}
结束
结束
私有的
#使用回调在操作之间共享公共设置或约束。
def set_产品
@product=product.find(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def产品参数
参数require(:product).permit(:title,:description,:image,:price,:category_id=>[])
结束
def search_参数
默认参数={}
如果登录,默认的参数合并({user\u id\u eq:current\u user.id})?
#这里有更多的逻辑
参数[:q]。合并(默认参数)
结束
结束