Ruby on rails 如何在rails中呈现表及其关联项目
我已经在rails中创建了脚手架项目和舞台。两者在rails中都有多对一关联。就像每个项目将有多个阶段,用户将有多个项目。我能够渲染项目将关联的用户id,但舞台将在每个用户上渲染。我能解决这个问题吗 project.rbRuby on rails 如何在rails中呈现表及其关联项目,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我已经在rails中创建了脚手架项目和舞台。两者在rails中都有多对一关联。就像每个项目将有多个阶段,用户将有多个项目。我能够渲染项目将关联的用户id,但舞台将在每个用户上渲染。我能解决这个问题吗 project.rb has_many :stages stage.rb belongs_to :project project show.html.erb,我在其中渲染项目的阶段 <div class="table-scroll"> <table>
has_many :stages
stage.rb
belongs_to :project
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>
stages\u 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
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
我希望舞台只呈现给他们的相关项目。我需要做哪些更改?在旁注中-不要将布尔值用作状态。这是一个可怕的db设计选择。一个字符串、一个整数或其他任何东西,甚至多个布尔列都是更好的选择。特别是因为您似乎使用了空状态,这意味着它甚至不是真正的布尔值。@max感谢您的建议。
def show
@project = Project.includes(:stages).find(params[:id])
@stages = @project.stages
end