has_many :through association issue in rails5
has_many :through association issue in rails5
user
has_many project
和 project
有很多 user
.
project_user
table 具有属性 project_id
和 user_id
.
为用户使用设计gem。
每当用户创建新项目时,项目都会由用户创建。但用户在 index.html.erb
视图中看不到项目。
这是我的代码:
project.rb
has_many :project_users
has_many :users, through: :project_users
project_user.rb
class ProjectUser < ApplicationRecord
belongs_to :user
belongs_to :project
end
user.rb
has_many :project_users
has_many :projects, through: :project_users
projects_controller.rb
def index
@projects = current_user.projects.all.order("created_at DESC")
end
def create
@project = current_user.projects.build(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to projects_url, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
params.require(:project).permit(:project_name, :from_date, :to_date)
index.html.erb#项目
<% @projects.each do |project| %>
<% end %>
form.html.erb(项目)
<%= form_with(model: project, :html => {:id => 'project-form-validation'}, local: true) do |form| %>
<div class="row select-date-wrapper">
<div class="field columns large-6">
<%= form.label :project_name,:class=>"required" %>
<%= form.text_field :project_name %>
</div>
<div class="field columns large-2">
<%= form.label :from_date,:class=>"required" %>
<%= form.text_field :from_date, class: 'datepicker', readonly: 'true' %>
</div>
<div class="field columns large-1"></div>
<div class="field columns large-2">
<%= form.label :to_date,:class=>"required" %>
<%= form.text_field :to_date, class: 'datepicker', readonly: 'true' %>
</div>
<div class="field columns large-1"></div>
</div>
<div class="row">
<div class="actions columns button-right">
<br>
<%= form.submit :class => "button primary" %>
</div>
</div>
<% end %>
您需要将创建的项目附加到用户的项目中。我认为您可以执行以下操作:
if @project.save
current_user.project_users.create(project: @project)
# rest of the code
end
user
has_many project
和 project
有很多 user
.
project_user
table 具有属性 project_id
和 user_id
.
为用户使用设计gem。
每当用户创建新项目时,项目都会由用户创建。但用户在 index.html.erb
视图中看不到项目。
这是我的代码: project.rb
has_many :project_users
has_many :users, through: :project_users
project_user.rb
class ProjectUser < ApplicationRecord
belongs_to :user
belongs_to :project
end
user.rb
has_many :project_users
has_many :projects, through: :project_users
projects_controller.rb
def index
@projects = current_user.projects.all.order("created_at DESC")
end
def create
@project = current_user.projects.build(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to projects_url, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
params.require(:project).permit(:project_name, :from_date, :to_date)
index.html.erb#项目
<% @projects.each do |project| %>
<% end %>
form.html.erb(项目)
<%= form_with(model: project, :html => {:id => 'project-form-validation'}, local: true) do |form| %>
<div class="row select-date-wrapper">
<div class="field columns large-6">
<%= form.label :project_name,:class=>"required" %>
<%= form.text_field :project_name %>
</div>
<div class="field columns large-2">
<%= form.label :from_date,:class=>"required" %>
<%= form.text_field :from_date, class: 'datepicker', readonly: 'true' %>
</div>
<div class="field columns large-1"></div>
<div class="field columns large-2">
<%= form.label :to_date,:class=>"required" %>
<%= form.text_field :to_date, class: 'datepicker', readonly: 'true' %>
</div>
<div class="field columns large-1"></div>
</div>
<div class="row">
<div class="actions columns button-right">
<br>
<%= form.submit :class => "button primary" %>
</div>
</div>
<% end %>
您需要将创建的项目附加到用户的项目中。我认为您可以执行以下操作:
if @project.save
current_user.project_users.create(project: @project)
# rest of the code
end