I'm new to Rails.
(我是Rails的新手。)
I have a table called TeamMemberships
that has 2 foreign keys: one to Users
and one to Teams
. (我有一个名为TeamMemberships
的表,该表具有2个外键:一个给Users
,一个给Teams
。)
I want to add a TeamMembership
element to the TeamMemberships
table from the show.html.erb
page for Teams
such that the Team
foreign key is the Team
that called the action and the User
will be determined by a form. (我想从show.html.erb
页面将Team
的TeamMembership
元素添加到Teams
的TeamMemberships
表中,以使Team
外键是调用该操作的Team
,而User
将由表单确定。)
When I try to add the TeamMembership
, the page redirects as expected, however, no table element is added and no error message is given. (当我尝试添加TeamMembership
,页面将按预期方式重定向,但是,未添加任何表元素,并且未给出错误消息。)
What could be causing this? (是什么原因造成的?)
Here are my routes: (这是我的路线:)
Rails.application.routes.draw do
resources :instructors
get 'static_pages/home'
get 'static_pages/adminLanding'
resources :audiences
resources :comments
resources :scores
resources :projects
resources :team_memberships
resources :team_types
resources :teams
resources :users
Rails.application.routes.draw do
resources :teams do
resources :team_memberships
end
end
root 'static_pages#home'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Here is the link from the teams/show.html.erb
page:
(这是来自teams/show.html.erb
页面的链接:)
<%= link_to 'Add a student', new_team_team_membership_path(@team.id) %>
Here is the new
method in the TeamMemberships
controller:
(这是TeamMemberships
控制器中的new
方法:)
def new
@team_membership = TeamMembership.new(team_id: params[:team_id])
@users = User.all
end
Here is the team_memberships/new.html.erb
file for the form:
(这是表单的team_memberships/new.html.erb
文件:)
<h1>New Team Membership</h1>
<%= form_for @team_membership do |f| %>
<%= f.select :user_id, options_for_select(@users.collect{ |student| [student.first_name + ' ' +student.last_name, student.id]}) %>
<%= f.submit %>
<% end %>
<%= link_to 'Back', team_memberships_path %>
Here is the create
method of the TeamMemberships
controller:
(这是TeamMemberships
控制器的create
方法:)
def create
@users = User.all
@team_membership = TeamMembership.new(team_membership_params)
respond_to do |format|
if @team_membership.save
format.html { redirect_to @team_membership, notice: 'Team membership was successfully created.' }
format.json { render :show, status: :created, location: @team_membership }
else
format.html { render :new }
format.json { render json: @team_membership.errors, status: :unprocessable_entity }
end
end
end
Thanks!
(谢谢!)
ask by astronautical_22 translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…