No other answers I found on that issue helped me.
I have a terms and conditions box and want to validate if it's checked on creation.
In my model:
validates :terms_and_conditions, :on => :create, acceptance: true, :allow_nil => false
In my view:
<%= f.check_box(:terms_and_conditions )%>
It always fails, no matter if the check_box is checked or not. I think it never actually sets the field and the problem is in my view, but I can't find it.
Log when submitting the form.
Processing by Users::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xNNmGTWSV4hkj0rrlZeOq+AiTlJ9sc9+cAT1Sy0bjhc=", "user"=>{"email"=>"sdasdr#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "name"=>"", "surname"=>"", "country_id"=>"", "language"=>"", "description"=>"", "register_as_company"=>"1", "terms_and_conditions"=>"1"}, "commit"=>"Sign up"}
Unpermitted parameters: name, surname, country_id, language, description, register_as_company, terms_and_conditions
[1m[35m (0.1ms)[0m begin transaction
[1m[36mUser Exists (0.3ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'sdasdr#gmail.com' LIMIT 1[0m
[1m[35m (0.1ms)[0m rollback transaction
[1m[36mCountry Load (0.2ms)[0m [1mSELECT "countries".* FROM "countries"[0m
Rendered devise/registrations/new.html.erb within layouts/application (6.1ms)
Rendered layouts/_auth_widget.html.erb (1.1ms)
Completed 200 OK in 127ms (Views: 22.8ms | ActiveRecord: 0.7ms)
You have not permitted terms_and_conditions attribute to be saved in the database. If you notice the server logs closely,
Unpermitted parameters: name, surname, country_id, language, description, register_as_company, terms_and_conditions
there are multiple attributes which are not permitted so all of them are not getting saved in database. terms_and_conditions goes as nil and results in error as the value submitted via form is ignored (because of unpermitted status).
This is related to Rails 4 Strong Parameters concept wherein only those attributes would be saved in database which are whitelisted/ permitted.
Checkout the information on Strong Parameters
Also, as per the server log I noticed that these attributes are part of User model which is a Devise model. Permitting attributes for Devise can be done in different ways,
you can refer to SO Question: Rails devise simple form not saving all fields for this.
Related
I'm trying to do the test signup at the end of lecture 113 in Upskill's Essential Web Developer course, however I get the following error and I have been over and over the previous lectures and cannot pinpoint where I have gone wrong.
Stripe::InvalidRequestError in Users::RegistrationsController#create
This customer has no attached payment source
Extracted source (around line #10):
8 def save_with_subscription
9 if valid?
10 customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
11 self.stripe_customer_token = customer.id
12 save!
13 end
Rails.root: /home/ubuntu/workspace/saasapp
Application Trace | Framework Trace | Full Trace
app/models/user.rb:10:in `save_with_subscription'
app/controllers/users/registrations_controller.rb:7:in `block in create'
app/controllers/users/registrations_controller.rb:3:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"/4EiUCLerdc0o+vIbZWWZzxC3cm1TSjyWGs/lYq/H4RiT6zwohUJUQnZaIrxADF2RiWcs6G3BDXiRDQT/bEa4Q==",
"plan"=>"2",
"user"=>{"email"=>"test#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}
Toggle session dump
Toggle env dump
Response
Headers:
None
LOG
Started POST "/users" for 81.140.28.63 at 2017-01-18 21:24:44 +0000
Cannot render console from 81.140.28.63! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GIGatf9j6proPQgjnjCwR6r6BBeDLCYa2lhOG453bQLwln1wTOEbVSPaPow6ZLGBcGpvX3Qm1Fw03vpQc0Xtsg==", "plan"=>"2", "user"=>{"email"=>"test#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.1ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "test#example.com"], ["LIMIT", 1]]
(0.2ms) rollback transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "test#example.com"], ["LIMIT", 1]]
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
Completed 500 Internal Server Error in 905ms (ActiveRecord: 0.9ms)
Stripe::InvalidRequestError (This customer has no attached payment source):
app/models/user.rb:10:in `save_with_subscription'
app/controllers/users/registrations_controller.rb:7:in `block in create'
app/controllers/users/registrations_controller.rb:3:in `create'
Rendering /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (9.5ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.3ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.7ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (43.3ms)
I would recommend dividing out the requests to see which part of it is actually failing. I suspect that the reason it's barfing is just because something else is happening and it's unable to manifest a meaningful error message.
Try this:
cus = Stripe::Customer.create(description: email)
self.stripe_customer_token = cus.id
cus.sources.create(source: stripe_card_token)
sub = Stripe::Subscription.create(customer: cus.id, plan: plan_id)
save!
I just hit the same issue.
I made the fix for the typo but still got the same error. It looks like the test card number used in the course video maybe another source for this
error too. I used a test number from the following page and all worked as expected.
https://stripe.com/docs/testing#cards
Just my 2cents!
Many thanks for taking the time to reply. So I gave it a few days and looked at it again last night and it turned out to be a rather simple and stupid mistake on my part.
In the lesson video and notes the instructor corrects a typo in the users.js file where he referenced an incorrect ID for the submit button in the form. He had typed
var submitBtn = $('#form-submit-btn');
but he had used the following in the form
var submitBtn = $('#form-signup-btn');
While I changed my users.js file to match the correction, somehow I had actually used #form-submit-btn' so when I corrected the users.js file per the lesson I actually caused the problem.
I've created a form using Simple Form in Rails to create new tests. My Test model has a has_and_belongs_to_many association with the Tool Model.
My code gets all tools and groups them into categories (a category is an attribute of a tool). It then creates a single select box for each category (as shown in the image).
- all_tools = Tool.all.group_by(&:category)
- all_tools.each do |category|
= f.association :tools,
collection: category.last,
label: category.first,
label_method: lambda { |tool| "#{tool.name} (#{tool.description})"},
input_html: { multiple: false }
Many select boxes
When choosing a tool from each select box and submitting the form, I get the following error:
found unpermitted parameter: tool_ids
and the there's only one id currently being submitted for the tool_ids instead of an array of ids which I need.
I know this works when the boxes are multiple selects but I only want the user to be able to select one tool from each category and have it submit all tool_ids in an array similar to the way it would if the user was selecting them from a multiple select box.
So my question is, how can I use multiple single select boxes to submit an array of tool_ids in Simple Form?
EDIT:
As requested, I've added the relevant code:
tests_controller:
def create
#test = Test.new(test_params)
#tools = Tool.all.select(:name).distinct
if #test.save
Activity.log(current_user, "Created test #{#test.id}")
redirect_to #test, notice: 'Test was successfully created.'
else
render :new, notice: 'Unable to save Test Details.'
end
end
private
# Only allow a trusted parameter "white list" through.
def test_params
params.require(:test).permit(
:date,
:start_time,
:description,
:machine,
:location,
:feeder_screw_diameter,
:notes,
:user_id,
:tool_ids => [],
:powder_ids => []
)
end
Log output when submitting:
Started POST "/tests" for 127.0.0.1 at 2016-05-08 13:24:52 +0100
Processing by TestsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"WKw+tQ1WKyxW7XZWCDvbvShCKnZ7iMY597P8eDGIvZEyis4ks7Mf4Lcu4vCf7q+fwtsReocmAlMGUrrhI4SgdQ==",
"test"=>{"start_time(1i)"=>"2016",
"start_time(2i)"=>"5",
"start_time(3i)"=>"8",
"start_time(4i)"=>"13",
"start_time(5i)"=>"23",
"description"=>"Test",
"machine"=>"Some machine",
"location"=>"Some Place",
"feeder_screw_diameter"=>"15",
"tool_ids"=>"21",
"powder_ids"=>["", "3"],
"user_id"=>"1",
"date"=>"11-05-2016",
"notes"=>"Some Notes"},
"button"=>""}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 500 Internal Server Error in 14ms (ActiveRecord: 0.4ms)
** [Airbrake] Notice was not sent due to configuration:
Environment Monitored? false
API key set? true
ActionController::UnpermittedParameters (found unpermitted parameter: tool_ids):
app/controllers/tests_controller.rb:201:in `test_params'
app/controllers/tests_controller.rb:70:in `create'
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.0ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (12.9ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (7.9ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (16.1ms)
Many thanks for any help!
I am taking over a project where Devise is in use. No signup needed because users are created when they provide their email for another purpose. Then the user receives an email with the link to password setup.
Now I want to also allow signup, but it still searches for an existing email, as seen in the log (several user models, the one used here is Traveler):
Started POST "/en/travelers" for 127.0.0.1 at 2015-10-02 09:07:09 +0200
Processing by DeviseInvitable::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"blah", "traveler"=>{"email"=>"test#test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up", "protocol"=>"https"}
Traveler Load (3.3ms) SELECT "travelers".* FROM "travelers" WHERE "travelers"."email" = 'test#test.com' ORDER BY "travelers"."id" ASC LIMIT 1
(1.7ms) BEGIN
Traveler Exists (2.4ms) SELECT 1 AS one FROM "travelers" WHERE "travelers"."email" = 'test#test.com' LIMIT 1
(1.6ms) ROLLBACK
Rendered devise/registrations/new.html.erb within layouts/auth/application (14.4ms)
Rendered layouts/auth/_flash.html.erb (0.3ms)
Rendered layouts/auth/_footer.html.erb (1.3ms)
Completed 200 OK in 282ms (Views: 153.6ms | ActiveRecord: 11.0ms)
I haven't seen anything in the routes or controllers that would make that happen, any ideas on where I could investigate?
I think that your traveller is invalid. E.g. traveller with this email exists. Check it and unique constraints.
Ruby on Rails 3.2
My form is creating a new distributor. I have added the gem 'bcrypt-ruby', '3.0.1', my database table has password_digest: string and my model has :password, :password_confirmation, and has_secure_password.
When I submit my form the error I get is Password digest can't be blank.
This is what is POSTed:
Started POST "/distributors" for x.x.x.x at 2014-03-14 12:59:07 -0700
Processing by DistributorsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x=", "distributor"=>{"company_name"=>"First Account", "company_website
"=>"First.com", "contact_name"=>"First", "contact_email"=>"first#moon.com", "contact_title"=>"owner", "company_phone"=>"555-665-5555", "company_region"=>"Asia Paci
fic", "company_address"=>"172 E Center Street", "company_country"=>"Cambodia", "company_city"=>"Covina", "company_state"=>"Not Applicable", "company_zip"=>"01970",
"sales_contact_name"=>"Bob", "sales_contact_email"=>"bobby#moon.com", "tech_contact_name"=>"Steph", "tech_contact_email"=>"steph#moon.com", "additional_name"=>"",
"additional_email"=>"", "additional_name2"=>"", "additional_email2"=>"", "area_served"=>["", "Bangladesh"], "terms_and_conditions"=>"1"}, "commit"=>"Register"}
Distributor Load (0.1ms) SELECT `distributors`.* FROM `distributors` WHERE `distributors`.`contact_email` = 'first#moon.com' LIMIT 1
(0.1ms) BEGIN
Distributor Exists (0.2ms) SELECT 1 AS one FROM `distributors` WHERE `distributors`.`contact_email` = 'first#moon.com' LIMIT 1
(0.1ms) ROLLBACK
Does someone know what I'm missing? Thank you
Your log is missing both the password and password_confirmation fields being set. It should look more like this
{"utf8"=>"✓", "authenticity_token"=>"dsdfhjdskhfsdfhjsdfhjsdfhjdsfhjsdfE=", "user"=>{"email"=>"email#provider.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Register"}
The hash you post to your distributors route does not contain any information about a password. It is not possible to create a distributor without a password, with the code you have written. So the error message is perfectly correct.
You need to add a password and a password_confirmation field to your webpage.
need to create a wallet for user after user sign up
have user model:
has_one :wallet, :dependent => :destroy
after_create :create_wallet
def create_wallet
self.wallet.create(params[:wallet])
end
and wallet model
class Wallet < ActiveRecord::Base
belongs_to :user
end
when sign up, I got this error back
Started POST "/users" for 127.0.0.1 at 2012-08-03 13:40:41 +0300
Creating scope :page. Overwriting existing method User.page.
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7rgoJQwWFFbo4Wv6gHF2AzwQpCQwB+Pp/kaNRw/YW/k=", "user"=>{"email"=>"rmagnum2002#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"}
Creating scope :page. Overwriting existing method Product.page.
(0.2ms) BEGIN
(0.3ms) SELECT 1 FROM `users` WHERE `users`.`email` = BINARY 'rmagnum2002#gmail.com' LIMIT 1
(0.3ms) SELECT 1 FROM `users` WHERE `users`.`auth_token` = 'S5R9LGhWVn6ut6Uh4h7KCA==' LIMIT 1
SQL (0.2ms) INSERT INTO `users` (`auth_token`, `created_at`, `email`, `password_digest`, `password_reset_sent_at`, `password_reset_token`, `updated_at`) VALUES ('S5R9LGhWVn6ut6Uh4h7KCA==', '2012-08-03 10:40:41', 'rmagnum2002#gmail.com', '$2a$10$KnwA.xrFY4z6eao7SmjHM.dq4ypmZZv3Rnk5LT9pg1jpiwxbl2YkK', NULL, NULL, '2012-08-03 10:40:41')
Creating scope :page. Overwriting existing method Wallet.page.
Wallet Load (0.1ms) SELECT `wallets`.* FROM `wallets` WHERE `wallets`.`user_id` = 4 LIMIT 1
(27.2ms) ROLLBACK
Completed 500 Internal Server Error in 158ms
NameError (undefined local variable or method `params' for #<User:0x9af5894>):
app/models/user.rb:39:in `create_wallet'
app/controllers/users_controller.rb:57:in `block in create'
app/controllers/users_controller.rb:56:in `create'
app/controllers/application_controller.rb:78:in `catch_not_found'
app/middleware/flash_session_cookie_middleware.rb:17:in `call'
Rendered vendor/bundle/ruby/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.8ms)
Rendered vendor/bundle/ruby/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.7ms)
Rendered vendor/bundle/ruby/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (3.1ms)
the code I built up is based on this answer
Rails 3: After_save create profile
what do I do wrong. Please help. Thank you.
The error undefined local variable or method 'params' clearly indicates that params are not available in your model.
Also, it's not a good idea to use params in your models. See this question Rails How to pass params from controller to after_save inside model.
Further, even if you got params in your model, params[:wallet] will not be available because that wasn't submitted from the web form.
# No 'wallet' key here.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7rgoJQwWFFbo4Wv6gHF2AzwQpCQwB+Pp/kaNRw/YW/k=", "user"=>{"email"=>"rmagnum2002#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"}
UPDATE
Creating wallet without params
has_one :wallet, :dependent => :destroy
after_create do |user|
user.create_wallet
end
Undefined local variable or method `params' for your user model