如何通过提交按钮更改默认布尔值?
How to change default boolean value via submit button?
用户可以选择两个提交按钮之一。如果他点击<%= f.submit :conceal %>
怎么让默认值从false变成true?
t.boolean "conceal", default: false
因为现在如果用户单击按钮 - 默认值保持为 false:
[1] pry(main)> Valuation.find(9)
Valuation Load (0.2ms) SELECT "valuations".* FROM "valuations" WHERE "valuations"."id" = ? LIMIT 1 [["id", 9]]
=> #<Valuation:0x007fdf26c736b8
id: 9,
conceal: false,
user_id: 1,
created_at: Thu, 23 Apr 2015 16:21:31 UTC +00:00,
updated_at: Thu, 23 Apr 2015 16:21:31 UTC +00:00,
likes: nil,
name: "Conceal/Private/Hide">
这没有用 <%= f.submit :conceal => true %>
。
class ValuationsController < ApplicationController
before_action :set_valuation, only: [:show, :edit, :update, :destroy, :like]
before_action :logged_in_user, only: [:create, :destroy]
def index
if params[:tag]
@valuations = Valuation.tagged_with(params[:tag])
else
@valuations = Valuation.order('RANDOM()')
end
end
def show
@valuation = Valuation.find(params[:id])
@commentable = @valuation
@comments = @commentable.comments
@comment = Comment.new
end
def new
@valuation = current_user.valuations.build
end
def edit
end
def create
@valuation = current_user.valuations.build(valuation_params)
if @valuation.save
track_activity @valuation
redirect_to @valuation, notice: 'Value was successfully created'
else
@feed_items = []
render 'pages/home'
end
end
def update
if @valuation.update(valuation_params)
track_activity @valuation
redirect_to @valuation, notice: 'Value was successfully updated'
else
render action: 'edit'
end
end
def destroy
@valuation.destroy
redirect_to valuations_url
end
def like
without_tracking do
@valuation.increment!(:likes)
end
@valuation.create_activity :like
flash[:success] = 'Thanks for sharing your Value!'
redirect_to valuation_path(@valuation)
end
private
def without_tracking
Valuation.public_activity_off
yield if block_given?
Valuation.public_activity_on
end
def set_valuation
@valuation = Valuation.find(params[:id])
end
def correct_user
@valuation = current_user.valuations.find_by(id: params[:id])
redirect_to valuations_path, notice: "Not authorized to edit this valuation" if @valuation.nil?
end
def valuation_params
params.require(:valuation).permit(:name, :conceal, :tag_list, :content, :commentable, :comment, :like)
end
end
更新
Started POST "/valuations" for 127.0.0.1 at 2015-04-23 15:27:35 -0400
Processing by ValuationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xd4ISNLZxjryLJfxvauWUANBzJthCNBzUwhtxseB/ird77mdYoKY/f9Skzb68lClaWOIUJXie9qC0jI1l+d98w==", "valuation"=>{"name"=>"PRIVATE AGAIN", "tag_list"=>""}, "commit"=>"conceal"}
[1m[36mUser Load (0.7ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mHabit Load (0.2ms)[0m SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ? [["user_id", 1]]
[1m[36mHabit Load (0.2ms)[0m [1mSELECT "habits".* FROM "habits"[0m
[1m[35mActsAsTaggableOn::Tag Load (0.2ms)[0m SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35mSQL (31.7ms)[0m INSERT INTO "valuations" ("name", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "PRIVATE AGAIN"], ["user_id", 1], ["created_at", "2015-04-23 19:27:36.015267"], ["updated_at", "2015-04-23 19:27:36.015267"]]
[1m[36mActsAsTaggableOn::Tag Load (0.1ms)[0m [1mSELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)[0m [["taggable_id", 15], ["taggable_type", "Valuation"]]
[1m[35m (10.8ms)[0m commit transaction
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35mSQL (1.7ms)[0m INSERT INTO "activities" ("action", "trackable_id", "trackable_type", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["action", "create"], ["trackable_id", 15], ["trackable_type", "Valuation"], ["user_id", 1], ["created_at", "2015-04-23 19:27:36.136688"], ["updated_at", "2015-04-23 19:27:36.136688"]]
[1m[36m (1.5ms)[0m [1mcommit transaction[0m
Redirected to http://0.0.0.0:3000/valuations/15
submit
方法接受一个参数来确定它将显示的名称,但这仅用于确定使用哪个按钮(如果有多个按钮)来提交表单。
您要做的是引入一个具有正确命名约定的隐藏字段,以便您可以使用 update_attributes
,或者您需要测试是否存在 params[:commit]
,如果存在,表示有人按下了那个按钮:
if (params[:commit] == 'conceal')
@valuation.conceal = true
end
用户可以选择两个提交按钮之一。如果他点击<%= f.submit :conceal %>
怎么让默认值从false变成true?
t.boolean "conceal", default: false
因为现在如果用户单击按钮 - 默认值保持为 false:
[1] pry(main)> Valuation.find(9)
Valuation Load (0.2ms) SELECT "valuations".* FROM "valuations" WHERE "valuations"."id" = ? LIMIT 1 [["id", 9]]
=> #<Valuation:0x007fdf26c736b8
id: 9,
conceal: false,
user_id: 1,
created_at: Thu, 23 Apr 2015 16:21:31 UTC +00:00,
updated_at: Thu, 23 Apr 2015 16:21:31 UTC +00:00,
likes: nil,
name: "Conceal/Private/Hide">
这没有用 <%= f.submit :conceal => true %>
。
class ValuationsController < ApplicationController
before_action :set_valuation, only: [:show, :edit, :update, :destroy, :like]
before_action :logged_in_user, only: [:create, :destroy]
def index
if params[:tag]
@valuations = Valuation.tagged_with(params[:tag])
else
@valuations = Valuation.order('RANDOM()')
end
end
def show
@valuation = Valuation.find(params[:id])
@commentable = @valuation
@comments = @commentable.comments
@comment = Comment.new
end
def new
@valuation = current_user.valuations.build
end
def edit
end
def create
@valuation = current_user.valuations.build(valuation_params)
if @valuation.save
track_activity @valuation
redirect_to @valuation, notice: 'Value was successfully created'
else
@feed_items = []
render 'pages/home'
end
end
def update
if @valuation.update(valuation_params)
track_activity @valuation
redirect_to @valuation, notice: 'Value was successfully updated'
else
render action: 'edit'
end
end
def destroy
@valuation.destroy
redirect_to valuations_url
end
def like
without_tracking do
@valuation.increment!(:likes)
end
@valuation.create_activity :like
flash[:success] = 'Thanks for sharing your Value!'
redirect_to valuation_path(@valuation)
end
private
def without_tracking
Valuation.public_activity_off
yield if block_given?
Valuation.public_activity_on
end
def set_valuation
@valuation = Valuation.find(params[:id])
end
def correct_user
@valuation = current_user.valuations.find_by(id: params[:id])
redirect_to valuations_path, notice: "Not authorized to edit this valuation" if @valuation.nil?
end
def valuation_params
params.require(:valuation).permit(:name, :conceal, :tag_list, :content, :commentable, :comment, :like)
end
end
更新
Started POST "/valuations" for 127.0.0.1 at 2015-04-23 15:27:35 -0400
Processing by ValuationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xd4ISNLZxjryLJfxvauWUANBzJthCNBzUwhtxseB/ird77mdYoKY/f9Skzb68lClaWOIUJXie9qC0jI1l+d98w==", "valuation"=>{"name"=>"PRIVATE AGAIN", "tag_list"=>""}, "commit"=>"conceal"}
[1m[36mUser Load (0.7ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mHabit Load (0.2ms)[0m SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ? [["user_id", 1]]
[1m[36mHabit Load (0.2ms)[0m [1mSELECT "habits".* FROM "habits"[0m
[1m[35mActsAsTaggableOn::Tag Load (0.2ms)[0m SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35mSQL (31.7ms)[0m INSERT INTO "valuations" ("name", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "PRIVATE AGAIN"], ["user_id", 1], ["created_at", "2015-04-23 19:27:36.015267"], ["updated_at", "2015-04-23 19:27:36.015267"]]
[1m[36mActsAsTaggableOn::Tag Load (0.1ms)[0m [1mSELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)[0m [["taggable_id", 15], ["taggable_type", "Valuation"]]
[1m[35m (10.8ms)[0m commit transaction
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35mSQL (1.7ms)[0m INSERT INTO "activities" ("action", "trackable_id", "trackable_type", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["action", "create"], ["trackable_id", 15], ["trackable_type", "Valuation"], ["user_id", 1], ["created_at", "2015-04-23 19:27:36.136688"], ["updated_at", "2015-04-23 19:27:36.136688"]]
[1m[36m (1.5ms)[0m [1mcommit transaction[0m
Redirected to http://0.0.0.0:3000/valuations/15
submit
方法接受一个参数来确定它将显示的名称,但这仅用于确定使用哪个按钮(如果有多个按钮)来提交表单。
您要做的是引入一个具有正确命名约定的隐藏字段,以便您可以使用 update_attributes
,或者您需要测试是否存在 params[:commit]
,如果存在,表示有人按下了那个按钮:
if (params[:commit] == 'conceal')
@valuation.conceal = true
end