【发布时间】:2018-04-11 14:39:16
【问题描述】:
我在复选框中有布尔函数。当复选框被选中并提交时,它将是真实的。现在我需要一个名为 unblock 的按钮,当我第一次按下它时,它必须为 true 并被提交,并且按钮名称必须更改为阻止。并且在第二次单击时它必须更改为 false。
我的控制器代码
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
我的查看代码
<%= 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(value: false), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
Unblock
<% end %>
<%else%>
<%= button_to user_path(value: true), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
block
<% end %>
<%end%>
控制器代码
def block
user = User.find(params[:id])
user.update(active: params[:value])
end
【问题讨论】:
-
到目前为止你尝试了什么?
-
@Harini 你只想要名为 block 和 unblock 的按钮而不是提交按钮和复选框吗?
-
@Gabbar 我想要一个必须更改布尔函数然后提交的按钮
标签: ruby-on-rails ruby checkbox boolean-operations