从列表中编辑项目只会从Rails

时间:2017-09-19 21:19:47

标签: ruby-on-rails ruby forms parameters edit

我无法在SO上或通过Google搜索找到答案。

我有一个在索引页面上显示的提交列表,由用户的项目显示页面制作。网址显示正确的ID,输入的数据成功。

但是,当我点击特定提交的“编辑”时,它会跳转到带有正确网址(ID)的编辑表单,但会显示为项目输入的第一个提交的值。删除提交时,它会成功删除该提交。如果删除第一个提交,则单击编辑时,下一个提交的值将显示并反映所有其他提交。我在这里不知所措 - 感谢帮助!

提交索引页面:

<div class="row wrapper">
  <h2 class="color-blue"><%= @project.title %> Submissions</h2>
  <% if @project.submissions.present? %>
    <% @project.submissions.each do |submission| %>
      <div class="submission">
        <ul>
          <li class="title">
            <%= link_to submission.request_title, project_submission_path(@project, submission) %>
          </li>
          <li class="actions">
            <span class="edit">
              <%= link_to 'edit', edit_project_submission_path(@project, submission), class: 'color-orange' %> |
            </span>
            <span class="delete">
              <%= link_to 'delete', project_submission_path(@project, submission), method: :delete, data: { confirm: 'Are you sure?' }, class: 'color-red' %>
            </span> |
            <span class="send">
              <%= link_to 'send request', '#', class: 'color-atlantis' %>
            </span>
          </li>
            </li>
        </ul>
      </div>
    <% end %>
  <% end %>
  <br>
  <%= link_to "Back to #{@project.title} project", @project, class: 'color-clue' %>
</div>

提交控制器:

class SubmissionsController < ApplicationController

  before_action :authenticate_user!
  before_action :set_project
  before_action :set_submission, only: [:destroy, :update, :edit, :create, :show]

  def index
  end

  def show
  end

  def new
    @submission = @project.submissions.new
  end

  def edit

  end

  def create
    @submission = @project.submissions.new(submission_params)
    @submission.user = current_user

    if @submission.save
      redirect_to @project, notice: 'Music submission was successfully sent.'
    else
      render :new
    end
  end

  def update
    if @submission.update(submission_params)
      redirect_to project_submissions_path(@project), notice: 'Music submission was successfully updated.'
    else
      render :edit
    end
  end

  def destroy
    @submission.destroy
    redirect_to project_submissions_path(@project), notice: 'Music submission was successfully deleted.'
  end

  private

    def set_project
      @project = current_user.projects.find(params[:project_id])
    end

    def set_submission
      @submission = @project.submissions.find_by(params[:submission_id])
    end

    def submission_params
      params.require(:submission).permit(:request_title, :request_music, :notes, :terms)
    end

end

2 个答案:

答案 0 :(得分:3)

我认为params属性名称中的问题。单击编辑按钮时检查服务器日志。是submission_id还是id?

答案 1 :(得分:1)

问题出在set_submission方法中。它应该是

def set_submission
  @submission = @project.submissions.find_by(id: params[:id])
end

def set_submission
  @submission = @project.submissions.find(params[:id])
end