无法正确显示Rails关联中的编辑和删除按钮

问题描述 投票:0回答:1

我在导轨上有两个脚手架。第一个是项目,第二个是阶段。项目与阶段有一对多的联系。我在表中的project#show中渲染项目及其阶段,每当我单击编辑阶段按钮时,始终都会渲染并更新第一阶段的编辑页面。我希望当用户单击舞台编辑时对当前舞台进行编辑。

stages_controller.rb

  def edit
    @stage = Stage.find(params[:project_id])
    @project = Project.find(params[:project_id])
  end
  def update
    respond_to do |format|
      if @stage.update(stage_params)
        format.html { redirect_to project_path(@project), notice: 'Stage was successfully updated.' }
        format.json { render :show, status: :ok, location: @stage }
      else
        format.html { render :edit }
        format.json { render json: @stage.errors, status: :unprocessable_entity }
      end
    end
  end

projects_controller.rb

  def show
    @project = Project.includes({stages: {tasks: :sub_tasks}}).find(params[:id])
    @stages = @project.stages
  end

routes.rb

  resources :projects do
    resources :stages do
      resources :tasks do
        resources :sub_tasks
      end
    end
  end

project show.html.erb

  <tbody>
      <% @stages.each do |stage| %>
        <tr class="stage">
          <td><%= stage.stage %></td>
          <td><%= stage.planned_start_date.strftime("%d-%m-%Y") %></td>
          <td><%= stage.planned_end_date.strftime("%d-%m-%Y") %></td>

          <td><%= link_to "Add Task", new_project_stage_url(@project, stage), :class=>"button primary small" %></td>

          <td><%= link_to 'Edit', edit_project_stage_path(@project), :class=>"button secondary small" %></td>
          <td><%# link_to 'Destroy', stage, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        </tr>

        <% stage.tasks.each do |task| %>
          <tr>
            <td class="text-center"><%= task.task_name %></td>
            <td><%= task.planned_start_date.strftime("%d-%m-%Y") %></td>
            <td><%= task.planned_end_date.strftime("%d-%m-%Y") %></td>

            <td><%= link_to "Add Sub Task", new_project_stage_task_sub_task_url(@project, stage, task), :class=>"button primary small" %></td>
            <td><%# link_to 'Edit', edit_task_path(task) %></td>
            <td><%# link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
          </tr>
</tbody>
ruby-on-rails ruby-on-rails-5
1个回答
0
投票

stages_controller.rb

  def edit
    @project = Project.find(params[:project_id])
  end
© www.soinside.com 2019 - 2024. All rights reserved.