Best way to pass variables from one form to another - ruby-on-rails

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

Related

rails 4- where to put user function

I am building a sample app for learning rails 4, and I'm a little confused on where I'm meant to build certain things. For example, I want to check if a user is logged in, and if so, display their account balance in the header (a partial).
Thanks to Michael Hartl's tutorial, I have a function to check a user's login status in the session helper, which is included in the application controller and can therefore be accessed in the partial.
Since the balance is tracked in the Users table, do I build a function get_balance in the Users model? Or should I create a function in the application helper? If I do build it in the application helper, is this auto-included in the application controller, or do I have to include it specifically? If I don't build the function in the model, can I still access the User object?
Thanks for your patience with a noob.
Since your users balance is a column in Users table, it is already there for you as a field (most possibly user.balance). And yes, this is where you should store it. You might use helpers for stuff that is related to general layout of your application and use combination of partial view and layout to spread it around.
Since it's already on your table, assuming your user is logged in, you could just call
current_user.balance
But it sounds like you want to add onto the data given,
I would suggest perhaps using a Rails decorator for your user model.
Basically a decorator adds an object-oriented layer of presentation logic to your Rails application.
I use the Draper Gem

rails best practice for a multistep form?

I want to create a mutlistep form in Rails 3. I've watched the railscasts episode on it, but I did not feel like he was using the best practice when creating the form. I felt like it was a sloppy way to accomplish the task. What is the best way to create a multistep form using the best practices in rails?
Your question is a little hard to answer without more information, and a true answer would be a detailed tutorial (like a Railscast) rather than a SO answer, but here's some thoughts to get you on your way.
There are two major approaches to multistep forms:
Use Javascript to display the form bit by bit
Create separate views and use create/update or similar to route the user from one to the next.
There are advantages to each method, depending on whether you want to support javascript, and what your requirements are about saving data in between sections.
Advantages of 1
Faster for the user to navigate from section to section (javascript hide/show is instantaneous)
Data is easily accessible is the user wants to refer to an earlier section
Simpler controller actions
Disadvantages of 1
Will not work for users who are not running javascript (and no progressive enhancement is really possible here other than displaying the form as a huge chunk).
Will require you to provide javascript-based navigation to move from section to section (only a disadvantage if you're new to .js)
Will require AJAX if you want to save the user's information between steps.
Without AJAX and javascript, the user is at the risk of losing a lot of entry if the user accidentally pressing the back button, etc.

Can I use symfony admin generator to manage two related modele at once

I'm in the process of learnign symfony and now I'm playing with the admin generator.
I'm doing a blog as test project.
I've managed to list my post & my comments but I was wondering if there was any way to have the comments crud below the post view instead of having to seperate pages ?
Thanks for reading
It should be noted that the template system that is available in the "Frontend", is also available in the admin generator. If you create /template files in your admin generator modules, whatever template file that is placed there will override the generated ones.
This being said, you can create highly customized interfaces in the admin generator using this method. Just override what you need, and let the admin generator handle the rest.
More information on how to use templates in the backend here:
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/12#chapter_12_templates_customization
You are going to have to do custom code for this. As soon as you start stepping away from what the admin generator gives you out of the box it is usually simpler to write your own code rather than trying to extend the admin generator code (unless it is really simple)
By using a custom form & the admin generator, you can generate some rather complicated admin pages. You can embed an entire subform for a related object, have it display the actual choices for one to many and many to many relationships, and lots more. Essentially, if you create a form, it will generate a page from it.
While you could probably figure out a way to embed a subform for each comment, I don't think that would be a good idea. Generally, you only want to embed one form within another for a one to one relation, such as a user_details form embedded in the user form.

Evaluation Form Creation

I'm taking a "cloud computing" course and have a few problems that I am trying to work through for the final project. I'll preface this with saying that i know nothing about this subject, and the few powerpoints I got off my online class don't really seem to point me in the right direction.
Basically what i need to do is have a page that allows a user to create a evaluation form. I have a general idea how to do it but I don't even know where to start in rails, I was hoping there was some stuff that Rails would do for me that might make it easier.
My assumption is that I could create a form that would allow entries for section headings.
Then when one occurs I need to allow actual question entries. then the user has the option of adding questions for the section. Then the user may add another heading, and questions.
When this "process" is complete, I need to allow a 2nd user to actually use the form for entries.
I'm assuming I somehow need to make a "string' object that will hold the text that I'm setting up, and then it sets up entries for each question.
I'm clueless where to start, I'll probably be asking a lot of questions. I don't want the solution I just need to get pointers, heading in the right direction.
the first step I'm thinking is finding a way to allow a "command" to do the equivalent of this but in the browser from another form:
rails generate scaffold Evaluation Topic:string rating:string comments:string
Is there a way to "create" a scaffold of a form using rails in a web browser?
It seems to me that you are mostly concerned with created forms using Rails.
Railscasts has some awesome tutorials on forms: Railscasts
After viewing some of those, you should see that Rails follows an MVC policy (more information here).
Since you mentioned the scaffold, it creates several controller actions. One of those is "new" (also "edit") which I believe would be the equivalent of creating the evaluation form.
You could then create a controller action(s) for users to fill out the evaluation form.
These links should clear up a few of your questions and get you used to the rails terminology for better questions later.
Good Luck!

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