How do I use nested attributes in rails 5? - ruby-on-rails

I have been trying to implement nested attributes in my rails application for some days now. I have searched and searched but all examples seems not to be working for me. I will like someone to help me with what I want to implement.
I have a User model that is powered by Devise gem. I also have a Location and Bio model.
This is the scenario... After a User registers, he can set his current location and Bio (This is done after Registration). I want the User to be able to Update those information at the same time using one form.
What I have tried... I created a non resource controller called UserPreference and created an Update and Create action that Updates the information supplied by the User in a Field_for form for each of the models. I have also used accepts_nested_attributes in the parent model which is the User model. Both of the other models are all a One to One relationships. Please, I have been on this for days now and checked through various resources here and online.
p.s: I might not have explained this very well but I will be pleased if someone can write out the solution step by step for me as I am still a noob in Rails. Thanks

Related

How to organize controllers with 2 user models

The current setup I have now is fresh and just installed Devise, and created 2 models. One called User and the other called Admin.
The question I have is that they both share some elements. Say like notifications. Whilst some are only to one of the models. In the past I have been taking the easy way out and not properly organizing them properly.
How do I structure the controllers so that they are up to date with proper standards?
If you have same relations on User and AdminUser it's because your AdminUser it's a system user too. Am I wrong? if this is the case, I would remove ActiveAdmin model adding on User model a role attribute. I think this is what you need to do.

Building a user register page

I am trying to setup user registration. I have been recommended not to use scaffold.
So far I have run this command to generate model for all profile information:
rails generate model User name email birthday:date zip_code time_zone sexuality career education religion politics children height does_user_smoke does_user_drink about_me:text career
I want the required sign up information from when a user first visits the website to be name, email, birthday, zip code, and password. I am not sure what the next step is. As far as building the page to have the users sign up. I know I have to edit app/views/users/new.html.erb. When I did the tutorials I ran scaffold which did the work for me, but now I have been told by several people to not user scaffold and just do everything yourself. But I haven't read a tutorial, nor can I find any examples on the internet that will walk me through building a user register page in rails.
So I don't know if I should go back to using scaffold or hopefully someone can point me in the right direction with starting on the user register page.
Yes, there are tutorials about authentication. I personally like Ryan Bates's screencast authentication from scratch. That tutorial should include everything you need.
As for the scaffolding, I think it really depends on what you're trying to achieve here. All scaffolding does is create the basic CRUD (create, read, update, delete) operations on a model for you, including controller, views and tests. Since you needed those views, for example, you could have used that. And prewritten tests are awesome.
However, in this case I would just go with the screencast and not use scaffolding. Scaffolding is nice for any model that doesn't need anything fancy like authentication, but only the basic CRUD operations.
Try to Analise that do you want views/controllers/models or all.
Since this problem is specific to user registration, so you may only need to have user registration view and user model and the controller that performs the registration logic.
Scaffolding is useful to automate your task specifically if you want a complete MVC functionality.
For ex:
If you use the controller generator, this will generate the controller action and the view.
rails g controller controllername new create
This will create new and create actions with their relevant views.

How Do I Create a User Profile With Devise?

I really like how devise offers an easy to use registration system out of the box but I'm having trouble extending it to do what I need. I need to create a public user profile for each user that shows their information like name, email, bio, and more info. I've done this in the past before with a users/show function but since devise doesn't provide any easily editable controllers, I'm having trouble figuring out how to do this. I've already run rails generate devise:views to copy the devise views to my app but I don't know where to go from here. Any help would be much appreciated.
Sounds like you want users to update their profile at the same time they create their account? If so, you can setup an associated Profile model with the User model. Using accepts_nested_attributes_for you can then create a record for the nested model on devise user registration submit/creation.
Here's a great screencast covering nested models and I also suggest you search other devise relate SO posts as this question has been discussed before.
There is an alternative approach, that is simpler to implement — only allow registered users edit/update their profile. This way you don't have to alter the Devise views and you can setup the various CRUD actions via a separate non-devise controller.
Throw in an Access Control List (ACL) solution such as CanCan (there are other alternatives too!) and you can even allow other users view profiles but deny access to edit/destroy etc.

Ruby on Rails - Create Entity with Relationship

I'm new to rails, so be nice.
I'm building a "rolodex" type application, and this question is about the best way to handle creating an entity along with several relationship entities at the same time.
For (a contrived) example:
My application will have a Person model, which has_one Contact_Info model. On the create.html.erb page for Person it makes sense for the user of my appliction to create the person, and the contact_info at the same time.
It doesn't seem right to include details for creating a contact directly in the create view/controller for person. What's the rails way to handle this?
Using nested attributes is the most common way to do this.
The actual documentation is here.
You want to use "Nested Forms". There is a great example of them in this blog post.
I'm also noob, but I had a similar issue with an app. I was using a tutor at the time and he basically said it was a good example of rails being opinionated. It sounds like you want to take the create action for two different models at the same time, which may be possible but probably very hard. Id suggest considering whether your data model could be modified, or find a way to make an acceptable user flow while collecting the data in different forms.
Update: while writing this the technical answer came in. Keep in mind, its perfectly okay to take the easy route if doing so helps you get the app out the door, and especially while you're still new.

Ruby on Rails User Setup

Currently, I have a 6-model ruby on rails application, that I added authlogic to.
The overall setup is
User :has_many categories, topics,messages
Categories has_many topics,
Topics has_many messages
(With and the corresponding opposite belongs_to links).
When I try to access current_user.categories.find(2), no results are returned in the controller.
Furthermore, when I try to run this
current_user.topics.find(params[:topic_id]).messages.build
Then,
#msg = current_user.messages.build(params[:message])
#msg.save
It doesn't save the user_id from the has_many.
All features of this program were working before the current_user directives were added in.
Am I making a mistake with the setup? Or with the execution?
Because the association isn't saving after the build, could I later add the user_id field in the model?
Sorry about all the questions, and thanks in advance.
I think your best bet is to go step-by-step. What does current_user return? Does the id of that user match one in your db? Does that user have any categories? Do any of them have an id of 2?
If you can isolate your problem to a single layer in your chained calls, it will be much easier to debug.
Thanks Kyle.
I've solved the problem using a filter in the model instead of using the controller to assign it on creation through association.
Current_user simply returns the record of the current user using authlogic.
I'm liking where the project is bow, and might deploy it after some visual tweaking, security, and more css :).
Callbacks and filters are amazing with whatever you are developing.
Also, if you need to get an variable from the application_controller to the model use the dollar sign ruby variable, not a class instance variable (at-sign).
Rails is so easy compared to a roll-your-own Php or sintra app.
Also, How many models are used for rails apps?

Resources