如何从 rails 中的 index#action 设置属性?

how to set attribute from index#action in rails?

project_site 模型有一个属性 submission_status。我想从 index#action 为相应的行设置该状态。我想在提交按钮上单击每一行以将相应的 submission_status 更改为 true。我如何在 rails 5.

中执行此操作

project_sites_controller.rb


def index
    @project_sites = current_user.project_sites
  end


  def new
    @project_site = current_user.project_sites.build
  end


  def create
    @project_site = current_user.project_sites.build(project_site_params)
    @users = current_user.director_id
    @projects = Project.where(user_id: @users)

    respond_to do |format|
      if @project_site.save
        format.html { redirect_to project_sites_url, notice: 'Attendance was successfully Uploaded.' }
        format.json { render :show, status: :created, location: @project_site }
      else
        format.html { render :new }
        format.json { render json: @project_site.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @users = current_user.director_id
    @projects = Project.where(user_id: @users)

    respond_to do |format|
      if @project_site.update(project_site_params)
        format.html { redirect_to project_sites_url, notice: 'Attendance was successfully updated.' }
        format.json { render :show, status: :ok, location: @project_site }
      else
        format.html { render :edit }
        format.json { render json: @project_site.errors, status: :unprocessable_entity }
      end
    end
  end

index.html.erb

 <table>
      <thead>
        <tr>
          <th>Uploaded Date</th>
          <th>Attendance File</th>
          <th>Submit Attendance</th>

        </tr>
      </thead>
      <tbody>
        <% @project_sites.each do |project_site| %>
          <tr>
            <td><%= project_site.created_at.strftime('%d-%m-%Y') %></td>
            <td><%= link_to "View Attendance", project_site.attendance.url, :class => "fi-page-export-csv" %></td>

            <td>
              <%= form_for ProjectSite.new do |f| %>
                      <%#f.hidden_field :project_site_id, value: project_site.id%>
                      <%=f.hidden_field :submission_status, value: true%>

                    <div>
                      <%= f.submit 'Submit', :class => 'button primary small float-right' %>
                    </div>

              <% end %>
          </tr>
        <% end %>
      </tbody>
    </table>
You have to pass the id of project_site for which you want to update the submission_status and on the controller updates the same project_site(id pass as hidden field), for this you need write **ajax** call
<table>
      <thead>
        <tr>
          <th>Uploaded Date</th>
          <th>Attendance File</th>
          <th>Submit Attendance</th>

        </tr>
      </thead>
      <tbody>
        <% @project_sites.each do |project_site| %>
          <tr>
            <td><%= project_site.created_at.strftime('%d-%m-%Y') %></td>
            <td><%= link_to "View Attendance", project_site.attendance.url, :class => "fi-page-export-csv" %></td>

            <td>
              <%= form_for ProjectSite.new do |f| %>
                      <%#f.hidden_field :project_site_id, value: project_site.id%>
                      <%=f.hidden_field :submission_status, value: true%>
                       <%= f.hidden_field :project_site_id, project_site.id%>

                    <div>
                      <%= f.submit 'Submit', :class => 'button primary small float-right' %>
                    </div>

              <% end %>
          </tr>
        <% end %>
      </tbody>
    </table>

为设置状态的操作添加路由

#routes.rb
resources :project_sites do
    put :set_submission_status, on: :member
end

定义设置状态的动作

#submission_status_controller.rb
def set_submission_status
    @project_site = ProjectSite.find(params[:id])
    @project_site.update(submission_status: true)
    redirect_to project_sites_path
end

将 form_for 替换为以下 link

#view
= link_to set_submission_status_project_site_path(project_site), method: :put