![](/img/trans.png)
[英]how can i render table inside table by their associated id in rails
[英]how can i render table with their associated project in rails
我在 Rails 中创建了脚手架项目和舞台。 两者在 Rails 中都有多对一的关联。 就像每个项目都有多个阶段,用户将有多个项目。 我能够呈现项目将关联的用户 ID,但舞台会在每个用户上呈现。 我可以解决这个问题吗?
项目文件
has_many :stages
舞台.rb
belongs_to :project
项目 show.html.erb 在那里我呈现项目的阶段
<div class="table-scroll">
<table>
<thead>
<tr>
<th>Stage</th>
<th>Responsibility</th>
<th>Status</th>
<th>Finance</th>
</tr>
</thead>
<tbody>
<% @stages.each do |stage| %>
<tr>
<td><%= stage.stage %></td>
<td><%= stage.responsibility %></td>
<% if stage.status == true %>
<td class="completed"><%= "Completed" %></td>
<% elsif stage.status == false %>
<td class="in-progress"><%= "In-Progress" %></td>
<% else %>
<td class="yet-to-start"><%= "Yet to Start" %></td>
<% end %>
<td><%= stage.finance %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
project_controller.rb
def index
@projects = current_user.projects.all.paginate(page: params[:page], per_page: 15)
end
def show
@project=Project.find(params[:id])
@stages = Stage.all
end
def new
@project = current_user.projects.build
end
def create
@project = current_user.projects.build(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, 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
stage_controller.rb
def index
@stages = Stage.all
end
def show
end
def new
@stage = Stage.new
@project = Project.find(params[:project_id])
end
def create
@project = Project.find(params[:project_id])
@stage = @project.stages.build(stage_params)
respond_to do |format|
if @stage.save
format.html { redirect_to project_stages_path, notice: 'Stage was successfully created.' }
format.json { render :show, status: :created, location: @stage }
else
format.html { render :new }
format.json { render json: @stage.errors, status: :unprocessable_entity }
end
end
end
我希望舞台只呈现给他们相关的项目。 我需要做什么改变?
def show
@project = Project.includes(:stages).find(params[:id])
@stages = @project.stages
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.