Restful Authentication plugin and Users model - ruby-on-rails

I have a question about using the restful-authentication plugin. I have it working just fine with the "out of the box" setup.
I am trying to add a user partial form nested into a form of another model. I can not seem to figure out how to have this form add a new user to the users model. I have other nested forms that work just fine.
I think the problem is that the user model does not have a user_controller but instead is supposed to use :url => user_profiles_path so when I nest this in another form it doesn't work.
I am just so lost, I need to figure out how to nest a new User form into a Unit/new form.
I am not sure if this makes sense, but any direction would be greatly appreciated.
Thanks

Unless you're working with a legacy application, try using AuthLogic for authentication instead...it'll save you a lot of headaches!

Related

How do I use nested attributes in rails 5?

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

Best way to pass variables from one form to another

I need to have a minimised form on the front page of my rails site which passes the values through to a full form (devise generated sign up form).
What I've currently done is a simple form_tag that uses a get method on new_user_registration_path (generated by devise). And then get the variables out of the url and populate the fields. But I feel this could be done a better way, perhaps with a variable and form_for?
I found this example but would prefer to keep to Rails conventions, which seems to be variables, plus I'm not about user sessions.
I'm just learning Rails, could anyone help out?
Edit: added screenshots to help clarify use case hopefully:
Data is entered here, but as more is needed (password etc) before a user can be created
it passes the entered variables to this full form on another page:
I'm not sure I get the use case, but if you don't want to use sessions, to pass data from one form to another, you can set the variables in your controller like so:
#variable = params[:variable]
redirect_to :new_form
Then in your second form you can use a hidden field setting the #variable and just access it in params again.
Another option is using the flash
Checkout Wicked gem. Its makes easier for you to create multi-step forms. so I think It would help you in this case as well.
Use wicked to make your Rails controllers into step-by-step wizards.
To see Wicked in action check out the example Rails app or watch the
screencast.
Many times I'm left wanting a RESTful way to display a step by step
process that may or not be associated with a resource. Wicked gives
the flexibility to do what I want while hiding all the really nasty
stuff you shouldn't do in a controller to make this possible. At it's
core Wicked is a RESTful(ish) state machine, but you don't need to
know that, just use it.
For more details, see:
wicked on github
RailsCasts on Creating Multi-step Wizard Style forms

How do I use Devise with 2 nested resources?

I'm trying to use Devise with 3 models all at once, but I'm feeling a little lost right now with the sign up strategy.
My model looks like this:
User has_one Client has_one VIP_Client
Using Simple_Form and accepts_nested_attributes_for, I'm able to create all 3 models at once in the same signup page, but I want to break this into 2 different pages: one for User-Client signup, and another one for User-Client-VIP_Client signup, because the logic is quite different from one to another - the VIP_Client needs a few more validations and all. Problem is, I'm not sure how to proceed from here. How do I make 2 separate forms which builds new Users and map to the same User resource to save it into the DB using Devise?
Rather than focus on Devise and its models could you use some kind of multi-step form to solve your problem? Check out...
http://railscasts.com/episodes?utf8=%E2%9C%93&search=wizard

RESTful Actions Best Practices

I've got a RESTful User model working well in Rails 3. I'd like to add a new option to create a new user based on information queried out of a LDAP server.
What I'd like advice on is how best to do this. Here's what I've thought up so far, but I don't know if it matches Rails best practices:
Edit the resource path of User to accept both GET and POST to a new view called "import_ldap_user".
Import LDAP user then presents a form which uses AJAX (POSTing to import_ldap_user) to allow the visitor to search for a person in LDAP. The results are displayed on the page and if acceptable, the user clicks "Create", which then calls /user/create.
Part of why this seems bad to me is:
I need to post a proper #user to /user/create, but I'm not sure if my AJAX call can produce a proper #user.
I don't know if it's a bad practice to add a new verb to the RESTful Users route.
I don't know if using an AJAX POST to import_ldap_users is a proper separation of concerns.
Any ideas? Any Rails perfectionists have opinions about how this should work?
What gets posted to /user/create isn't an #user object but rather its attributes. A scaffolded create action will probably have something akin to #user.new(params[:user]), which just pulls the user attributes that were posted and creates a new object based on that.
Even if your AJAX call doesn't provide the attributes in a manner that can be processed by the new method, you can simply modify your create such that it manipulates the post data.
As for best practices, this is definitely something I've thought about in the past but I don't know if there's a "correct" answer. I think having a new view which posts to the create method is perfectly acceptable, you could also create a new controller if you want to strictly follow the CRUD pattern.
Definitely a good question and if anyone has a better answer I'd love to hear it.

Making forms in Rails

So far I've built a simple form for a user using the form_for method to wrap around my user model.
But if I'm going to create a form now which doesn't doesn't map directly to any particular model, should I still be using form_for?
You probably just want form_tag.
There are a few plugins which allow you to create an active record like model without having to have a database behind it. I would point you to a plugin that gives you this functionality, but I'm not sure which ones are still compatible with Rails 2.x.
If you were to go this route you could create a view mode, use form_for as you would normally and get all the validation stuff thrown in.
I've used this to great effect for things like login forms and the like.

Resources