Home Date failing to update, but returning true for Update (Rails ActiveRecord)
Reply: 1

Date failing to update, but returning true for Update (Rails ActiveRecord)

JCDJulian
1#
JCDJulian Published in 2017-12-07 10:48:49Z

I have a Rails model called user that has a birthday field with type date in Postgres.

My first issue is that I'm not sure how to update the value of a user. If I pass in a string like "10/10/1980", it does not update.

The second issue is that even when the birthday is not updated, Rails returns true for the User.update(user_params) action

My two questions:

  1. How can I update a date field?
  2. How can I make sure that rails throws an error if an incorrect date is passed in?

How can I update a date field?

Update method from controller below. Fairly standard from scaffolding.

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      puts(user_params)
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

Edit: Params:

  def user_params
    params.require(:user)
          .permit(:points, :payment_option, :first_name, :last_name,
                  :payment_email, :phone_number, :facebook_id, :profile_pic_url,
                  :locale, :timezone, :gender, :email, :country, :city,
                  :age_group, :birthday, :employment_status, :occupation,
                  :education_level, :income_bracket)
  end

Rails log (note that I added a puts(user_params) after the update statement.

Started PATCH "/api/v1/users/1" for 127.0.0.1 at 2017-12-07 19:16:37 +0800
Processing by UsersController#update as JSON
  Parameters: {"user"=>{"birthday"=>"12dsafkljfsldk"}, "id"=>"1"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
Can't verify CSRF token authenticity.
{"birthday"=>"10/10/1900"}
   (0.2ms)  BEGIN
   (0.1ms)  COMMIT
  Rendering users/show.json.jbuilder
  Rendered users/_user.json.jbuilder (0.6ms)
  Rendered users/show.json.jbuilder (1.6ms)
Completed 200 OK in 31ms (Views: 5.9ms | ActiveRecord: 3.1ms)

I was hoping that I could use before_update to parse the date string into a Date object before saving, but it doesn't look like self.birthday is even showing up in the model.

Aniket Shivam Tiwari
2#
Aniket Shivam Tiwari Reply to 2017-12-07 12:17:36Z

Place this line in your ApplicationController

 protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }

If it doesn't work then. Try to skip the action

skip_before_action :verify_authenticity_token
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299903 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO