gpt4 book ai didi

ruby-on-rails - Rails 4 - 多对多类别不会保存到数据库

转载 作者:数据小太阳 更新时间:2023-10-29 08:47:59 24 4
gpt4 key购买 nike

我有 3 个表,分别称为 pin(帖子)、genres(类别)和 genre_pin(pin 和 genres 之间是多对多的,因为 pin 可以有多种类型)。

我正在尝试构建创建表单,这将允许用户在 pin 上选择任意数量的流派并保存/创建。创建工作正常,没有错误,但它没有将任何关系记录保存到 genre_pin 表中。我很确定我可能在这里遗漏了一些非常明显的东西。

我使用的术语是“图钉”和“流派”,但您可以将它们视为与“帖子”和“类别”相同。

引脚模型

class Pin < ActiveRecord::Base

belongs_to :user
belongs_to :type
has_many :replies
has_many :genre_pins
has_many :genres, :through => :genre_pins

accepts_nested_attributes_for :genres, allow_destroy: true, reject_if: proc { |genre| genre[:id].blank? }

validates :user_id, presence: true
validates :type_id, presence: true
validates :title, presence: true
validates :description, presence: true

end

类型模型

class Genre < ActiveRecord::Base

has_many :genre_pins
has_many :pins, :through => :genre_pins

validates :title, presence: true

end

GenrePin 模型

class GenrePin < ActiveRecord::Base

belongs_to :pin
belongs_to :genre

validates_uniqueness_of :pin_id, :scope => :genre_id

end

接下来,在我的 pin#new View 中,我有以下形式:

固定新 View

<%= form_for :pin, url: pins_path do |f| %>

<div class="small-12 columns">
<%= f.label :title %>
<%= f.text_field :title %>
</div>

<div class="small-12 columns">
<%= f.label :description %>
<%= f.text_area :description, :rows => 10 %>
</div>

<div class="small-12 columns">
<%= f.label :type %>
<%= f.collection_select :type_id, Type.order(:title), :id, :title, include_blank: false %>
</div>

<div class="small-12 columns">
<%= f.label :genres %>
<%= f.collection_select :genre_ids, Genre.order(:title), :id, :title, {}, { :multiple => true } %>
</div>

<div class="small-12 columns">
<%= f.submit "Create Pin", :class => "button" %>
</div>

<% end %>

该 View 工作正常,并从流派表中获取可用流派。但是,当我去保存表格时,记录(pin)被创建,但与流派的关系却没有。我在这里错过了什么?不会抛出任何错误,只是什么也没有发生。我假设 Rails 应该通过与类型关联的引脚创建这些 PinGenre 记录。

引脚 Controller (创建)

def create

@pin = Pin.new(pin_params)
@pin.user_id = current_user.id
@pin.save

if @pin.save
flash[:success] = "Pin successfully created"
redirect_to @pin
else
flash[:warning] = @pin.errors.full_messages.to_sentence
redirect_to :action => "new"
end

end

private

def pin_params

params.require(:pin).permit(:title, :description, :type_id, :genre_ids)

end

这里我还需要采取进一步的步骤吗?记录是否需要由我自己明确创建?

感谢您的帮助和耐心等待。我希望我已经清楚地阅读了这篇文章。迈克尔。

Schema.rb

ActiveRecord::Schema.define(version: 0) do

create_table "genre_pins", force: true do |t|
t.integer "pin_id"
t.integer "genre_id"
end

create_table "genres", force: true do |t|
t.string "title", null: false
end

create_table "pins", force: true do |t|
t.integer "user_id", null: false
t.string "title", limit: 160, null: false
t.text "description", null: false
t.decimal "latitude", precision: 10, scale: 8
t.decimal "longitude", precision: 11, scale: 8
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "deleted_at"
t.integer "type_id", null: false
end

create_table "profiles", force: true do |t|
t.integer "user_id", null: false
t.string "first_name", null: false
t.string "last_name", null: false
t.string "gender"
t.string "email"
t.text "bio"
t.decimal "latitude", precision: 10, scale: 8
t.decimal "longitude", precision: 11, scale: 8
t.string "image", limit: 2000
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end

create_table "replies", force: true do |t|
t.integer "pin_id", null: false
t.integer "user_id", null: false
t.text "reply", null: false
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end

create_table "saves", force: true do |t|
t.integer "pin_id", null: false
t.integer "user_id", null: false
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end

create_table "settings", force: true do |t|
t.integer "user_id", null: false
t.boolean "show_email", null: false
t.boolean "show_telephone", null: false
t.integer "timezone", null: false
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end

create_table "types", force: true do |t|
t.string "title", null: false
end

create_table "users", force: true do |t|
t.string "provider", limit: 45, null: false
t.string "provider_id", limit: 45, null: false
t.string "oauth_token", limit: 1000
t.datetime "oauth_expires_at"
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end

end

生成的查看源代码

<form accept-charset="UTF-8" action="/pins" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="3VO3kVR/62JFf6dJs+yyPTPvYJb3fsUSLRFljSE0Jdk=" /></div>

<div class="small-12 columns">
<label for="pin_title">Title</label>
<input id="pin_title" name="pin[title]" type="text" />
</div>

<div class="small-12 columns">
<label for="pin_description">Description</label>
<textarea id="pin_description" name="pin[description]" rows="10">
</textarea>
</div>

<div class="small-12 columns">
<label for="pin_type">Type</label>
<select id="pin_type_id" name="pin[type_id]"><option value="2">Available</option>
<option value="1">Wanted</option></select>
</div>

<div class="small-12 columns">
<label for="pin_genres">Genres</label>
<input name="pin[genre_ids][]" type="hidden" value="" /><select id="pin_genre_ids" multiple="multiple" name="pin[genre_ids][]"><option value="5">Ambient</option>
<option value="4">Electronic</option>
<option value="2">Indie</option>
<option value="3">Jazz</option>
<option value="6">Metal</option>
<option value="1">Rock</option></select>
</div>

<div class="small-12 columns">
<input class="button" name="commit" type="submit" value="Create Pin" />
</div>

</form>

最佳答案

您需要告诉您的 Controller “genre_ids”应该是一个数组:

params.require(:pin).permit(:title, :description, :type_id, genre_ids: [])

但是我希望您会收到另一个错误(未知属性 gen_ids)。如果发生这种情况,请告诉我。

关于ruby-on-rails - Rails 4 - 多对多类别不会保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21625929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com