首页 > 解决方案 > 如何在索引操作中呈现部分表单以更新记录的布尔值?导轨 5

问题描述

我正在尝试在每个 do 块内呈现部分表单,以更新其布尔值和枚举值。我似乎无法将记录的 id 传递给表单本身。它无法识别表单对象。

我正在使用 Rails 5.2 和最新的 Simple_Form。我在 bs 4 下拉菜单中渲染了一个部分,它只会公开对象的布尔值和枚举器属性。

index.html.erb

<% @posts.each.with_index(1) do |post, index| %>
  <div class="card bg-light mb-3 posts-card-border-reset" style="max-width: 20rem;">
  <div class="card-header post-card-header"><span class="badge badge-purple">App</span> #<%= index %>
  <div class="dropdown dropleft" style="float: right;">
    <button class="bttn-material-circle bttn-sm bttn-default" id="dropdown_post_1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      <i class="fas fa-align-justify caret-color"></i>
    </button>
    <div class="dropdown-menu post-sub-menu-dropdown" aria-labelledby="dropdown_post_1">
          <%= render 'applicant_status_update_form', local: post %>
      </div>
    </div>
  </div>
</div>
<div class="card-body">
  <h5 class="card-title">
    <i class="fas fa-user-tie"></i> <%= post.user_first_name + " " + post.user_last_name %>
  </h5>
  <p class="card-text"><i class="fas fa-at"></i> <%= post.user.email %></p>
  </p>

  <div class="post-card-divider"></div>
  <small class="text-muted">
    <i class="far fa-clock"></i> <%= post.created_at.strftime('%l:%M %p %m/%d/%Y') %></small>
  </div>
</div>
<% end %>

形式

<%= simple_form_for(@post, :url => admin_applicant_update_application_status_path, :method => :post) do |f| %>
 <div class="dropdown-item">
<span class="dropdown-header"><%= f.input :is_post_reviewed, as: :boolean, boolean_style: :inline %> <small>Mark As Reviewed</small></span>
  </div>
  <div class="dropdown-divider"></div>
  <div class="dropdown-item">
    <ul class="list-group">
       <li><p class="font-weight-bold">Application Status</p></li>
    </ul>
    </div>
  <div class="dropdown-item">
    <span><%= f.input :status, as: :radio_buttons %></span>
  </div>
  <div class="dropdown-item">
    <%= f.button :submit, "Update Status", class: 'bttn-simple bttn-md bttn-royal' %>
  </div>
<% end %>

post_application.rb

class PostApplication < ApplicationRecord
  include Friendlyable
  belongs_to :user

  enum status: { pending: 'pending', accepted: 'accepted', denied: 'denied' }, _prefix: :status
end

admin_applicants_controller

class AdminApplicantsController < ApplicationController
  before_action :authenticate_admin!

  def index
@posts = Kaminari.paginate_array(current_admin.posts.collect(&:post_applications).flatten).page(params[:page]).per(15)
  end

  def update_application_status
    @post = PostApplication.find(params[:id])
permitted_columns = params.require(:post).permit(:is_application_reviewed, :status)
    if @post.update_attributes(permitted_columns)
      flash[:success] = "#{@post.first_name}'s Application Status Updated!"
      redirect_to admin_applicants_path
    else
      flash[:danger] = "#{@post.first_name}'s failed to update! Try again"
      redirect_to admin_applicants_path
    end
  end
end

路线

  resources :admin_applicants, :only => [:index] do
    post :update_application_status
  end

boolean/enum 属性应该是可变的,并且表单应该发布并应用更新。

标签: ruby-on-railsrubyactiverecord

解决方案


您可以render按照此处将变量直接传递给。在你的情况下:

<%= render 'applicant_status_update_form', post: post %>

还可以使用以下内容:

<%= render 'applicant_status_update_form', object: post, as: 'post' %>
<%= render 'applicant_status_update_form', locals: { post: post } %>

所有结果都相同:使post局部变量可用于您的局部变量。

从那里,您需要切换@postpost您的部分,并且所有内容都应按需要显示。

希望对您有所帮助-让我知道您的进展情况。


根据您的评论更新

如果您在更新操作中没有通过 ID 找到帖子,我想我已经看到了问题:您将 url 传递admin_applicant_update_application_status_path给没有 ID 的表单。

为了快速修复,您可以使用:

<%= simple_form_for(post, :url => admin_applicant_update_application_status_path(id: post.id), :method => :post) do |f| %>

更好的是更新您的路线以在update_application_status路径中获取 ID。所以,切换你routes.rb使用:

resources :admin_applicants, :only => [:index] do
  post 'update_application_status/:id'
end

然后你的表格:

<%= simple_form_for(post, :url => admin_applicant_update_application_status_path(post.id), :method => :post) do |f| %>

如果您有兴趣阅读,文档的这一部分将涵盖上述内容。

希望对你有用!


推荐阅读