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.
Related
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
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
I was using backbone standalone for some time but currently I am trying to integrate it with Rails. Until now I used underscore templates and the question would be if it is possible to use Rails view helpers inside the template and if it is smart thing to do at all?
Update: Here is a simple example what I am talking about.
I have a list of messages and I have a MessageView for each message, I want to render the avatar thumbnail of the message author, link to his profile and description when the message was posted. Also I use markdown for the message content. With underscore templates I don't have access to the helpers to achieve this so I am forced to create methods on the model itself which feels really wrong...
You should take a look at the EJS Embedded JavaScript Framework, which provides rails-like standard view helpers like link_to, url_for, and other form tags.
Of course, you will have to translate your custom rails templates in js, but it's a start !
I ran into the same problem where I wanted to reuse my templates between Backbone and Rails. I ran into stache before: https://github.com/agoragames/stache
You can read more about the setup here: http://slainer68.wordpress.com/2011/09/20/partial-reuse-between-rails-js-the-easy-way/
Right out of the box, your underscore templates are pure javascript, so, in that sense, you can't really embed rails helpers into them. You can, however, make those templates ejb's (or whatever templating system you use) and have rails render them. With so little information, it's impossible to figure out what your app does, but it does feel weird to me to do that. I think, typically, your javascript templates are used for rendering html on the host side after some js functionality. Maybe a better description of what you are trying to accomplish?
Update ...
So you have some set of relationships between messages and authors in your rails models correct? You'd do a similar thing in your backbone models. So, you've got a User model, and a Message model. User has_many Messages, and Message has_one User. You can model that out in backbone as well... see my answer here:
Backbone set collection attribute (for the url)
You just need to describe the relationship on the backbone side.
I have two models User(main document) and Contact(embedded document in User). I'm showing a list of contacts when displaying a user. I was wondering if there is a suggested way of creating an in place editable form for all the contacts that are shown on the same page.
Really appreciate an sort of examples.
You're going to have to employ a bit of Javascript to hook into the view to update the object via AJAX. There are ways to do this using ember-rails right out of the box, but you can pick any framework you like. Essentially, you'll need to be able to do a RESTful update of the user, making use of the remote: true parameter on your Rails form.
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!