Reputation:
The user can choose of one of two submit buttons. If he clicks <%= f.submit :conceal %>
how can the default value be made to change from false to true?
t.boolean "conceal", default: false
because right now if the user clicks the button - the default value remains 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">
This didn't work <%= 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
Upvotes: 0
Views: 479
Reputation: 211690
The submit
method takes an argument for what name it will show up as, but that's only useful for determining which button, if there's more than one, was used to submit the form.
What you want to do is introduce a hidden field that has the right naming convention so you can use update_attributes
, or you need to test for the presence of params[:commit]
which, if present, means someone pressed that button:
if (params[:commit] == 'conceal')
@valuation.conceal = true
end
Upvotes: 1