Comments (2)
(I see that you've written @user.User.create
, but I assume that's just a typo and it's actually @user = User.create
)
User.create
and Company.create
always returns the object, even though the validations fail.
I'm not quite sure, but it seems to me that you need a transaction here:
def post
Company.transaction do
@company = Company.create!(
:name => @input.name,
:sub_domain => @input.subdomain)
@user = User.create!(
:company_id => @company.id,
:first_name => @input.first_name,
:last_name => @input.last_name,
:email => @input.email,
:password => @input.password)
end
rescue
@errors = [@company, @user].compact.map(&:full_messages).flatten
render :errors
else
redirect Login
end
create! will raise an error if there's a validation error. The transaction block works such that if an error is raised inside it, it will rollback any changes. So if a validation failed on either @Company or @user, none of them will actually be saved. If you don't have the transaction there, you might end up with one Company being created, which is not connected to any User.
Because the rescue captures any error, it's a possibility that @Company or @user is nil, so [@company, @user].compact
removes those nils.
The else
-block isn't much used in Ruby, but I like it: If no exception is raised, then the else block is called. In this case: If all the validations passes and the data is saved to the DB, we redirect to Login.
There's probably other ways to solve this, so I've also written a message to the rubyonrails-talk mailing-list (the message is currently being moderated) and the Camping mailing-list, and I'll get back to you if someone comes up with a better solution.
from camping.
I got it to work. Turns out it was a lack of ActiveRecord knowledge and not am issue with camping as I expected. Your examples helped and after reviewing the active record docs I see what I was doing wrong. Thanks for the help!
from camping.
Related Issues (20)
- Rename Master Branch to Main (Again) HOT 1
- Fix Reloader HOT 3
- Gem dependencies are not fully documented in the ReadMe HOT 11
- Camping Server isn't Properly Tested.
- Add simpler DSL support for straight up mapping requests to a camping app.
- Add Markaby back as a dependency
- Add RAKE task to release new gem
- Make passkeys the default authentication option HOT 2
- Getting error when trying out camping book section 02 on models HOT 1
- RubyGems page still lists camping.rubyforge.org as homepage HOT 5
- Sub directories in Apps won't let you redefine URLs without being sad
- Camping only looks for views in a views directory in the root, and shallowly, should look for views in each app too.
- does the IRC channel work? HOT 3
- unable to run camping HOT 3
- Ruby 3.3 support? HOT 4
- Receiving syntax errors when using camping HOT 2
- CI: Gemfile complains about platforms HOT 1
- Camping Tests automated integration tests don't catch missing upstream gem issues HOT 2
- Camping new command offers no feedback upon success.
- Camping CLI has wrong feedback for -h command
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from camping.