ruby-on-rails - 如何在索引操作中呈现部分表单以更新记录的布尔值?导轨 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 属性应该是可变的,并且表单应该发布并应用更新。
解决方案
您可以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
局部变量可用于您的局部变量。
从那里,您需要切换@post
到post
您的部分,并且所有内容都应按需要显示。
希望对您有所帮助-让我知道您的进展情况。
根据您的评论更新
如果您在更新操作中没有通过 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| %>
如果您有兴趣阅读,文档的这一部分将涵盖上述内容。
希望对你有用!
推荐阅读
- python - 如何从嵌套的 xml 创建 pandas DataFrame
- jquery - 'load' 在另一个 'load' 回调中不起作用,但 'ready' 可以
- javascript - 整理动态对象
- .net-core - 调用 PerformanceCounterCategory.Exists() 在 docker 中抛出 System.PlatformNotSupportedException
- laravel-4 - 在我的本地服务器中设置后,Laravel 主页返回 404
- mysql - 如何通过sql在json中搜索[]
- powerbi - Power BI Embedded,仅通过查看权限生成嵌入令牌
- python - 用 BRISK 替换 SIFT 是 OpenCV 导致错误
- azure - 我们可以使用powershell实现天蓝色自动修复自定义操作,如果可能的话,最好的实践应该是什么?
- python - 在pytest中运行测试用例时如何显示断点而不是断言错误