Home>
I want to solve
I want to display validation when the button is pressed on the edit screen.
I'm trying to save to DB with assign_attributes in the action.
(Validation before transition to the edit confirmation screen)
module Api
module V1
class SuAccountsController</pre>
<p><br />
The parameter is POSTed to the "update_confirm" action in the API.</p>
<pre><code data-language = "params">app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
Started POST "/ api/v1/su_accounts/update_confirm" for 127.0.0.1 at 2019-12-05 12:36:02 +0000
Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.0/127.255.255.255, :: 1
Processing by Api :: V1 :: SuAccountsController # update_confirm as JSON
Parameters: {"su_account" =>{"id" =>1, "group_name" =>"good department", "image_url" =>"", "su_company_attributes" =>{"su_account_id" =>1, "company_name" =>"", "company_hp" =>"https://hoge.com", "establishment_year" =>2019, "establishment_month" =>11, "establishment_date" =>1, "representative" =>"Taro Hoge" , "business_description" =>"Development of Hogehoge", "capital" =>2000000, "capital_unit" =>"us_dollar", "address" =>"Huge City, Hoge" ",
"user_attributes" =>{"email" =>"[email protected]", "last_name" =>"Hagetaro", "first_name" =>"Hagematsu", "tel" =>nil}}}
And in@ su_account.assign_attributes (update_su_account_params)
in the action
SuAccount Load (1.1ms) SELECT `su_accounts`. * FROM` su_accounts` WHERE `su_accounts`.`deleted_at` IS NULL AND` su_accounts`.`id` = 1 LIMIT 1
↳ app/controllers/api/v1/su_accounts_controller.rb: 31: in `set_su_account '
SuCompanyInfo Load (0.6ms) SELECT `su_company_infos`. * FROM` su_company_infos` WHERE `su_company_infos`.`deleted_at` IS NULL AND` su_company_infos`.`su_account_id` = 1 LIMIT 1
↳ app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
(0.4ms) BEGIN
↳ app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
SuCompanyInfo Update (1.6ms) UPDATE `su_company_infos` SET` su_company_infos`.`su_account_id` = NULL, `su_company_infos`.`updated_at` = '2019-12-05 12: 36: 02.686532' WHERE` su_company_infos`.`id` = 1
↳ app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
(0.6ms) ROLLBACK
↳ app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
Completed 500 Internal Server Error in 112ms (ActiveRecord: 12.3ms | Allocations: 26144)
ActiveRecord :: NotNullViolation (Mysql2 :: Error: Column 'su_account_id' cannot be null):
app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
I am trying to save it.
Columns that can't be saved because they are null are columns that you don't want to update.
I don't think assign_attributes should be saved in the first place.
@su_account
, validate without saving,
I want to receive error contents in json.
Tell us if you can tell why it is stored.
Thank you.
* Strong parametersu_account_id
has the same result whether or not it exists.
Added model.
I feel the relationship is suspicious. .
class SuAccount</pre>
<pre><code>class SuCompanyInfo {establishment_month.presence || establishment_date.presence}
validates: establishment_month,
presence: true, if:->{establishment_year.presence}
validates: establishment_date,
presence: true, if:->{establishment_year.presence}
# Representative
validates: representative,
length: {maximum: 50}# Business description
validates: business_description,
length: {maximum: 500}
# Capital
validates: capital,
length: {maximum: 20},
numericality: {
only_integer: true,
allow_blank: true
}
# Currency unit
validates: capital_unit,
presence: true, if:->{capital.presence}
# Location
validates: address, length: {maximum: 200}
##
# enums
##
enum capital_unit: {
us_dollar: 1,
yen: 2,
yuan: 3
}
attribute: establishment_year
attribute: establishment_month
attribute: establishment_date
end
class User
-
Answer # 1
Related articles
- ruby on rails - i want to save the purchase information in the db
- ruby on rails - i want to save text information in rails
- ruby on rails - i want to save data in an intermediate table
- ruby on rails - i want to launch rails with a new file
- ruby on rails 6 - i want to save data from one form to multiple tables at the same time
- ruby on rails 5 - can i update with just the update action?
- ruby - i want to display specific columns in rails in descending order of numbers
- ruby - cannot save to database
- [ruby on rails] how to write when getting a model nested in multiple stages with includes
- ruby on rails - when i run rails s, i get
- ruby - i want to save the prototype and move the page
- ruby on rails - i installed ruby with rbenv, but the version does not change
- ruby - the last db setting doesn't work when deploying rails app on aws
- ruby on rails 5 - scope by specifying conditions for parent-child relationship
- ruby on rails 6 - about rails routing error no route matches [get] "/"
- ruby - about implementation of tag function in rails
- ruby on rails - i want to display the posted images in a random order (get information from the record)
- ruby on rails - [rails] i'm implementing a search function using form_with, but i'm having trouble with the search results not b
- ruby on rails - i would like to add an automatic address input function to the new registration screen
- ruby - [rails] i want to find the date difference using the model created_at
Related questions
- please tell me how to pass json information in nuxtjs
- ruby on rails - [rails] i want to pass json from controller to html (erb)
- ruby on rails - no data when using v-if
- javascript - about error when importing vue-router
- ruby - how to delete cookies
- json - when turning a multidimensional array with v-for
- ruby on rails - i want to know how to return from rails api mode to non-api mode (normal rails)
- convert json format data acquired using axios with vue-cli from utc to jst
- phpunit - is it possible to go to see the string in the vue component with assertsee ()?
- javascript - run multiple times with js + each do
With model relations
Solved by adding
update_only: true
likeReferenced articles
When accepting has_one associations_accepts_nested_attributes_for and assigning with attributes, it is destroyed if there is no id