Ruby 在 Rails - 通过关联新建模型对象
Ruby On Rails - New model object through association
我有三个模型 "Event"、"Team" 和 "User",它们具有各种 ActiveRecord 关联,但在创建新事件对象并将其与 team_id 关联时遇到问题.也许我在关系中遗漏了一些东西。它们的定义如下:
class User < ActiveRecord::Base
has_many :teams
has_many :events, through: :teams
end
class Team < ActiveRecord::Base
has_many :events
has_many :users
end
class Event < ActiveRecord::Base
belongs_to :team
has_many :users, through: :teams
end
class EventsController < ApplicationController
def new
@event = Event.new
end
def create
@event = current_team.events.build(event_params)
if @event.save
flash[:success] = "Event created!"
redirect_to @event
else
render 'new'
end
end
class TeamsController < ApplicationController
def new
@team = Team.new
end
def create
@team = current_user.teams.build(team_params)
if @team.save
flash[:success] = "Team created!"
redirect_to @team
else
render 'new'
end
end
当我提交创建新事件表单时,事件控制器中触发了错误,因为无法识别 current_team.events。我对 RoR 比较陌生,所以非常感谢任何帮助!
从您的代码来看,您的 EventsController
中似乎没有定义 current_team
。如果您从表单中获取团队 ID,您可以这样做:
current_team = Team.find(params[:team_id])
然后它应该可以工作了。
您在用户和团队之间加入 table 吗?从那个例子中,这两个模型之间存在多对多关系,但我没有看到定义的关系。检查 Rails guides
在团队模型中,作为连接 table 您应该执行以下操作
class Team < ActiveRecord::Base
belongs_to :event
belongs_to :user
end
在事件模型中:
class Event < ActiveRecord::Base
has_many :teams
has_many :users, through: :teams
end
我仍然不知道这是否是您真正想要的,如果团队 table 是一个连接 table 并且您希望它像一个连接一样,那么这应该没问题,但我建议您再次检查您的关系并检查 rails 指南以完全了解您在这里必须做什么
我终于明白是怎么回事了。我试图以定义 current_user 的方式定义 current_team,但是,current_user 只是当前登录会话中的一个用户。
因此,我走了一条不同的路,并在 link_to 表单中为新事件传递了 team_id 参数。这允许我在表单中的 hidden_field 中调用 team_id 并将其保存到数据库中。
方法在这个回复中解释得很好:Pass Parameters in Link_to
感谢大家的帮助!
事件控制器
class EventsController < ApplicationController
def new
@event = Event.new(:team_id => params[:team_id])
end
def create
@event = Event.new(event_params)
if @event.save
flash[:success] = "Event created!"
redirect_to @event
else
render 'new'
end
end
private
def event_params
params.require(:event).permit(:date, :name, :location, :venue, :team_id)
end
end
link:
<%= link_to "Create new event", newevent_path(:team_id => @team.id), class: "btn btn-md" %>
表单中的隐藏字段
<%= f.hidden_field :team_id %>
我有三个模型 "Event"、"Team" 和 "User",它们具有各种 ActiveRecord 关联,但在创建新事件对象并将其与 team_id 关联时遇到问题.也许我在关系中遗漏了一些东西。它们的定义如下:
class User < ActiveRecord::Base
has_many :teams
has_many :events, through: :teams
end
class Team < ActiveRecord::Base
has_many :events
has_many :users
end
class Event < ActiveRecord::Base
belongs_to :team
has_many :users, through: :teams
end
class EventsController < ApplicationController
def new
@event = Event.new
end
def create
@event = current_team.events.build(event_params)
if @event.save
flash[:success] = "Event created!"
redirect_to @event
else
render 'new'
end
end
class TeamsController < ApplicationController
def new
@team = Team.new
end
def create
@team = current_user.teams.build(team_params)
if @team.save
flash[:success] = "Team created!"
redirect_to @team
else
render 'new'
end
end
当我提交创建新事件表单时,事件控制器中触发了错误,因为无法识别 current_team.events。我对 RoR 比较陌生,所以非常感谢任何帮助!
从您的代码来看,您的 EventsController
中似乎没有定义 current_team
。如果您从表单中获取团队 ID,您可以这样做:
current_team = Team.find(params[:team_id])
然后它应该可以工作了。
您在用户和团队之间加入 table 吗?从那个例子中,这两个模型之间存在多对多关系,但我没有看到定义的关系。检查 Rails guides
在团队模型中,作为连接 table 您应该执行以下操作
class Team < ActiveRecord::Base
belongs_to :event
belongs_to :user
end
在事件模型中:
class Event < ActiveRecord::Base
has_many :teams
has_many :users, through: :teams
end
我仍然不知道这是否是您真正想要的,如果团队 table 是一个连接 table 并且您希望它像一个连接一样,那么这应该没问题,但我建议您再次检查您的关系并检查 rails 指南以完全了解您在这里必须做什么
我终于明白是怎么回事了。我试图以定义 current_user 的方式定义 current_team,但是,current_user 只是当前登录会话中的一个用户。
因此,我走了一条不同的路,并在 link_to 表单中为新事件传递了 team_id 参数。这允许我在表单中的 hidden_field 中调用 team_id 并将其保存到数据库中。
方法在这个回复中解释得很好:Pass Parameters in Link_to
感谢大家的帮助!
事件控制器
class EventsController < ApplicationController
def new
@event = Event.new(:team_id => params[:team_id])
end
def create
@event = Event.new(event_params)
if @event.save
flash[:success] = "Event created!"
redirect_to @event
else
render 'new'
end
end
private
def event_params
params.require(:event).permit(:date, :name, :location, :venue, :team_id)
end
end
link:
<%= link_to "Create new event", newevent_path(:team_id => @team.id), class: "btn btn-md" %>
表单中的隐藏字段
<%= f.hidden_field :team_id %>