Ruby on rails 复选框功能到按钮
我在复选框中有布尔函数。选中并提交复选框后,它将为true。现在我需要一个名为unblock的按钮,当我第一次按下它时,它必须为true并被提交,并且按钮名称必须更改为block。第二次单击时,它必须更改为false 我的控制器代码Ruby on rails 复选框功能到按钮,ruby-on-rails,ruby,checkbox,boolean-operations,Ruby On Rails,Ruby,Checkbox,Boolean Operations,我在复选框中有布尔函数。选中并提交复选框后,它将为true。现在我需要一个名为unblock的按钮,当我第一次按下它时,它必须为true并被提交,并且按钮名称必须更改为block。第二次单击时,它必须更改为false 我的控制器代码 def index @users = User.all end def edit end def update respond_to do |format| if @user.update(user_params) format.
def index
@users = User.all
end
def edit
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
else
format.html { render :edit }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:active)
end
def block
user = User.find(params[:id])
user.update(active: params[:value])
end
我的视图代码
<%= form_with(model: user, local: true) do |form| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :active %>
<%= form.check_box :active %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
更新 你可以简单地做到这一点
<% if user.active? %>
<%= button_to user_path(id: user.id, value: false), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
Unblock
<% end %>
<% else %>
<%= button_to user_path(id: user.id, value: true), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
block
<% end %>
<% end %>
所以代码看起来像这样
<div class="field">
<%= form.label :active %>
<%= form.hidden_field :active, 'false' %>
<%= form.check_box :active, true %>
</div>
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UKbxyz...==", "active"=>"true", "commit"=>"Submit"}
当取消选中复选框时
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UKbxyz...==", "active"=>"false", "commit"=>"Submit"}
标签本身创建表单,所以模型的错误不应该出现在表单中
所以这里我已经修改了你的代码,请看一看
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% if user.active? %>
<%= button_to user_path(value: false, id: user.id), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
Unblock
<% end %>
<%else%>
<%= button_to user_path(value: true, id: user.id), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
Block
<% end %>
<%end%>
更新
由于您已经更新了using block方法来更改用户的active
状态,因此在这种情况下,请不要忘记使用按钮使用此路径
和方法
示例:-假设您的路径是user\u block\u path
然后:-
到目前为止,您尝试了什么?@Harini是否只想要名为block and unblock的按钮,而不是submit按钮和checkbox?@Gabbar我想要一个必须更改布尔函数然后提交的按钮
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UKbxyz...==", "active"=>"false", "commit"=>"Submit"}
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% if user.active? %>
<%= button_to user_path(value: false, id: user.id), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
Unblock
<% end %>
<%else%>
<%= button_to user_path(value: true, id: user.id), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
Block
<% end %>
<%end%>
def update
if @user.update(active: params[:value])
respond_to do |format|
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.html { render :edit }
end
end
end
<%= button_to user_block_path(value: false, id: user.id), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
Unblock
<% end %>
def block
user = User.find(params[:id])
user.update(active: params[:value])
end