Ruby on rails 在Rails 4中使用has_至的多态类别
我只是在学习Rails,在为事件和用户配置文件分配样式(类别)时遇到了困难。风格是瑜伽的一种类型,设置为多态性,这样我就可以有活动、老师或这种风格的爱好者 在事件窗体上,字段呈现但不保存到数据库。我正在尝试使用集合_select助手,可以进行多种选择。表单提交并创建事件,但不会将任何内容保存到“样式”表中。这是建立多态关联的正确方法吗 Style.rbRuby on rails 在Rails 4中使用has_至的多态类别,ruby-on-rails,ruby-on-rails-4,polymorphism,has-many-through,polymorphic-associations,Ruby On Rails,Ruby On Rails 4,Polymorphism,Has Many Through,Polymorphic Associations,我只是在学习Rails,在为事件和用户配置文件分配样式(类别)时遇到了困难。风格是瑜伽的一种类型,设置为多态性,这样我就可以有活动、老师或这种风格的爱好者 在事件窗体上,字段呈现但不保存到数据库。我正在尝试使用集合_select助手,可以进行多种选择。表单提交并创建事件,但不会将任何内容保存到“样式”表中。这是建立多态关联的正确方法吗 Style.rb has_many :stylings, :dependent => :destroy has_many :events, :throu
has_many :stylings, :dependent => :destroy
has_many :events, :through => :stylings, :source => :styleable, :source_type => 'Event'
has_many :profiles, :through => :stylings, :source => :styleable, :source_type => 'Profile'
has_many :users, :through => :stylings, :source => :styleable, :source_type => 'User'
belongs_to :styleable, :polymorphic => true
belongs_to :style
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
<%= f.label :stylings, :value => 'Styles', class: 'o-label--quiet', :required => true %>
<%= collection_select(:event, :stylings, Style.all, :id, :name, {:prompt=> 'Select style(s)'}, {:multiple => true, :class=>'o-input--quiet js-styles'}) %>
def new
@event = Event.new
@locations = Location.all
@teachers = Profile.all
@styles = Style.all
end
def create
@event = current_user.events.build(event_params)
@event.length = @event.end_time - @event.start_time
@event.location_id = @event.location.id
if @event.save
redirect_to event_path(@event)
else
render 'new'
end
end
def edit
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
end
def update
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
if @event.update(event_params)
redirect_to event_path(@event)
else
render 'edit'
end
end
private
def event_params
params.require(:event).permit(:name, :image, :description, :start_date, :end_date, :start_time, :end_time, :all_day, :repeat, :category, :length, :link, :location_id, :user_id, :stylings => [])
end
样式设置.rb
has_many :stylings, :dependent => :destroy
has_many :events, :through => :stylings, :source => :styleable, :source_type => 'Event'
has_many :profiles, :through => :stylings, :source => :styleable, :source_type => 'Profile'
has_many :users, :through => :stylings, :source => :styleable, :source_type => 'User'
belongs_to :styleable, :polymorphic => true
belongs_to :style
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
<%= f.label :stylings, :value => 'Styles', class: 'o-label--quiet', :required => true %>
<%= collection_select(:event, :stylings, Style.all, :id, :name, {:prompt=> 'Select style(s)'}, {:multiple => true, :class=>'o-input--quiet js-styles'}) %>
def new
@event = Event.new
@locations = Location.all
@teachers = Profile.all
@styles = Style.all
end
def create
@event = current_user.events.build(event_params)
@event.length = @event.end_time - @event.start_time
@event.location_id = @event.location.id
if @event.save
redirect_to event_path(@event)
else
render 'new'
end
end
def edit
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
end
def update
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
if @event.update(event_params)
redirect_to event_path(@event)
else
render 'edit'
end
end
private
def event_params
params.require(:event).permit(:name, :image, :description, :start_date, :end_date, :start_time, :end_time, :all_day, :repeat, :category, :length, :link, :location_id, :user_id, :stylings => [])
end
Event.rb
has_many :stylings, :dependent => :destroy
has_many :events, :through => :stylings, :source => :styleable, :source_type => 'Event'
has_many :profiles, :through => :stylings, :source => :styleable, :source_type => 'Profile'
has_many :users, :through => :stylings, :source => :styleable, :source_type => 'User'
belongs_to :styleable, :polymorphic => true
belongs_to :style
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
<%= f.label :stylings, :value => 'Styles', class: 'o-label--quiet', :required => true %>
<%= collection_select(:event, :stylings, Style.all, :id, :name, {:prompt=> 'Select style(s)'}, {:multiple => true, :class=>'o-input--quiet js-styles'}) %>
def new
@event = Event.new
@locations = Location.all
@teachers = Profile.all
@styles = Style.all
end
def create
@event = current_user.events.build(event_params)
@event.length = @event.end_time - @event.start_time
@event.location_id = @event.location.id
if @event.save
redirect_to event_path(@event)
else
render 'new'
end
end
def edit
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
end
def update
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
if @event.update(event_params)
redirect_to event_path(@event)
else
render 'edit'
end
end
private
def event_params
params.require(:event).permit(:name, :image, :description, :start_date, :end_date, :start_time, :end_time, :all_day, :repeat, :category, :length, :link, :location_id, :user_id, :stylings => [])
end
Profile.rb
has_many :stylings, :dependent => :destroy
has_many :events, :through => :stylings, :source => :styleable, :source_type => 'Event'
has_many :profiles, :through => :stylings, :source => :styleable, :source_type => 'Profile'
has_many :users, :through => :stylings, :source => :styleable, :source_type => 'User'
belongs_to :styleable, :polymorphic => true
belongs_to :style
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
<%= f.label :stylings, :value => 'Styles', class: 'o-label--quiet', :required => true %>
<%= collection_select(:event, :stylings, Style.all, :id, :name, {:prompt=> 'Select style(s)'}, {:multiple => true, :class=>'o-input--quiet js-styles'}) %>
def new
@event = Event.new
@locations = Location.all
@teachers = Profile.all
@styles = Style.all
end
def create
@event = current_user.events.build(event_params)
@event.length = @event.end_time - @event.start_time
@event.location_id = @event.location.id
if @event.save
redirect_to event_path(@event)
else
render 'new'
end
end
def edit
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
end
def update
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
if @event.update(event_params)
redirect_to event_path(@event)
else
render 'edit'
end
end
private
def event_params
params.require(:event).permit(:name, :image, :description, :start_date, :end_date, :start_time, :end_time, :all_day, :repeat, :category, :length, :link, :location_id, :user_id, :stylings => [])
end
events/_form.html.erb
has_many :stylings, :dependent => :destroy
has_many :events, :through => :stylings, :source => :styleable, :source_type => 'Event'
has_many :profiles, :through => :stylings, :source => :styleable, :source_type => 'Profile'
has_many :users, :through => :stylings, :source => :styleable, :source_type => 'User'
belongs_to :styleable, :polymorphic => true
belongs_to :style
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
has_many :stylings, :as => :styleable, :dependent => :destroy
has_many :styles, through: :stylings
<%= f.label :stylings, :value => 'Styles', class: 'o-label--quiet', :required => true %>
<%= collection_select(:event, :stylings, Style.all, :id, :name, {:prompt=> 'Select style(s)'}, {:multiple => true, :class=>'o-input--quiet js-styles'}) %>
def new
@event = Event.new
@locations = Location.all
@teachers = Profile.all
@styles = Style.all
end
def create
@event = current_user.events.build(event_params)
@event.length = @event.end_time - @event.start_time
@event.location_id = @event.location.id
if @event.save
redirect_to event_path(@event)
else
render 'new'
end
end
def edit
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
end
def update
@event = Event.find(params[:id])
@locations = Location.all
@users = User.all
@teachers = Profile.all
@styles = Style.all
if @event.update(event_params)
redirect_to event_path(@event)
else
render 'edit'
end
end
private
def event_params
params.require(:event).permit(:name, :image, :description, :start_date, :end_date, :start_time, :end_time, :all_day, :repeat, :category, :length, :link, :location_id, :user_id, :stylings => [])
end
schema.rb
create_table "stylings", force: :cascade do |t|
t.integer "style_id"
t.integer "styleable_id"
t.string "styleable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
检查日志(在开发中,log/development.log
)。它们是否显示所传递的正确参数?它们是否显示由强参数过滤掉的任何参数?有验证错误吗?您的事件
模型中是否有接受\u嵌套的\u属性\u用于:样式
?我的直觉是你的事件参数也需要更改:styleings=>[]
为:styleings=>[:id]
。啊,他们说“unpermitted parameter:styleings”添加这两个参数(接受嵌套的属性\u用于:styleings
和:styleings=>[:id]
)生成EventsController中的NoMethodError#为nil:NilClass创建未定义的方法'each'
-再次删除:id,我回到以前的状态,日志显示“事件”=>{“名称”=>“接受参数”、“类别”=>“认证”、“样式”=>“2”…
,但仍然未允许的参数:样式