Nested forms Ruby on Rails - ruby-on-rails-3

I have a model Person and a model Option. Option belongs_to person, and Person has_many options. In the form for creating a new Person I want to be able to add several Options before you save the Person. What is the best way to do this?

Go for rails casts episode 196 and 197 . they have detailed explation and are the best to implement this kind of functionality
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2

It isn't necessarily the best solution, but Ryan Bates' nested_form library makes managing dynamic form elements for associated models quite easy. Easy to install and the usage directions in the README sound like they cover your basic case.

You might find my question, which was answered, and for which I post more or less complete solution files for, to be of use with nested associations:
Why submitting to wrong action when implementing "favorites", modeled after Michael Hartl's following / followers in Rails Tutorial

Related

Is there a workaround for strong parameter hell?

I just inherited a RoR 3.2 app and am trying to get it working on 4.2
I am going to put on my sarcastic hat for a second, just so I can feel better. Instead of having a single line in a single file to protect specific fields from mass-assignment, "Strong" Parameters requires bloating up controllers and heaven help you if a controller uses multiple models or a model is used by multiple controllers or need nested attribute whitelisting. This is the exact opposite of DRY and KISS.
That is better. Okay, so the question is, besides getting rid of mass assignment completely, which is sounding really good right about now, is there a sane way to use it or get around it. From what I understand that gem that brings back attr_accessible won't work in Rails 5 which is where this app is heading.
I understand the Ruby object model and can make a ton of modules that controllers can mixin, but that is just ugly and still error prone.
Any advice or hints would be welcome.
Why is that every new Rails feature involves more boilerplate spread over multiple files? If I wanted Java, I know where to find it. The stupidity of getting rid of the powerful and clean link_to_function in favor of using a tangled mess of callbacks almost made me quit, but adding that function back is trivial. Hopefully when the client wants the inevitable upgrade to Rails 5 I can talk him into something more sane and move things bit by bit to a sane web framework.
What you're after is "form objects". There's a great railscast episode on them. You may also be interested in the reform gem
Edit: looks like there's a free version of that railscast episode on youtube: https://www.youtube.com/watch?v=SvL_aZt3zyU

Writing a test for a Rails 3 model method that involves multiple associations

I'm new to the concept of test-driven development (and fairly new to Rails). I'm stuck with how to approach writing a test for my application.
As a backgrounder, the web app is a peer evaluation tool where individual groups are assigned surveys where they are asked to evaluate their peers. So a sample question would look like this:
Rate your peers on time management.
- Peer 1: __
- Peer 2: __
- Yourself: __
How I have this set up internally is as so:
I have a Survey model with a has_many association to an Assignment model (which controls the visibility of surveys among groups), which acts as a join model for Survey and a Group model. In the Group model there is a has_many association with Users.
Because multiple groups can be assigned the same survey, I have to find out which Group a certain user is asked to evaluate. (So the survey would be the same, but the people that show up as in the sample above would change from user to user).
I have decided to create a method, assignees_for_user(user), which would return, essentially, the user's group(s) that have been assigned to the survey.
I want to be able to unit test this method. I am using rspec and factory_girl so far. I have a few questions:
Is my approach even correct design-wise?
Should I test this? If so, I'll need to create instances for Assignment, Group, and User, right?
What's the best way I can stub/mock these instances without persisting them onto the database? Or will I have to?
Thanks!
This answer was more complicated than I expected (and to be honest, I guess that's why no one answered it :P)
You can use frameworks like mocha to mock things like find: How to test ActiveRecord witout Rails?
I ended up using rr instead. But when it comes to activerecord associations, I still haven't found a good way to do it because I don't fully understand the ActiveRecord association proxy objects. So I guess my takeaway in all of this is, honestly, not to waste my time mocking associations in the first place: As an old adage goes, you can't unit test something that isn't your own!
Can I suggest that refactoring the business logic from both the activerecord model (think of it as a database adaptor) and controller (think of it as a GUI adaptor) to plain old ruby objects (PORO) in the lib/ is the way to go.
Hexagonal Rails by Matt Wynne, Rails Conf 2013 Building Extractable Libraries in Rails by Patrick Robertson, Making ActiveRecord Models Thin and Gary Bernhardt are good places to start.

Authentication system in Rails 3 . Devise / omniauth or self coded?

I am a newbie in ROR developement. I am trying to create my own application with the help of Ruby on Rails tutorial 2nd edition. I made many reseaches. I need your comments about devise or any other authentications system or as the book offers us to write our own authentication systems.
What are your ideas? Is it wasting time to write your own authentication system?
i'm quite new to RoR myself but i used Devise so far (and according to the book i'm currently reading it is worth it).
You could use the Railscasts about Devise if you want an introduction to it:
http://railscasts.com/episodes/209-introducing-devise
And as it is quite easy to learn and well featured i'd say it is pretty overkill to write your own.
I am a huge advocate of Devise, and also of not recreating the wheel. My rebuttal question would be why go through the trouble of creating your own authentication solution, when in just a few short bash commands, Devise will be installed and running. The configuration options alone are enough to choose Devise, not to mention that almost every part of the engine has the ability to be overridden with your own code.
There are hundreds of blog posts out there that can easily get you started with running Devise. As always with any gem, I would recommend starting out with browsing the source code, and then turning to the wiki for the answers that you seek. As #krizz mentioned, Ryan Bates' Railscasts are always a great place to turn when you need to see it setup.
And of course, you can always turn back here to the stack when you get stuck.

Rating system for multiple models in Application

I need suggestion for rating system implementation. I have Blogs, Discussions, Comments in my system, so each member can make +1 to current blog post or discussion entry, comment etc. I know that there is acts_as_ratable, but this is no what I need.
Any suggestions, what can I use to keep my code DRY?
P.S - I know it can be handled using polymorphic associations, but I'm searching for basic solution to integrate.
Thanks in advance!
This is a perfect case for Rails' polymorphic associations -- several different types are "ratable".
Good doc on this here:
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
And great screencast here:
http://railscasts.com/episodes/154-polymorphic-association

Expose function in ruby on rails

I am looking at a rails app and at the top of every controller there is a block of code that looks something like this
expose(:var) {Model.find params[:var_id]}
I understand what is inside the block just fine but...
I cannot find any documentation on what the expose function does where it comes from or anything I have tried searching the project and using the searchable rails docs.
I would love to know what it does, can someone please tell me or point me to the docs.
This is probably referencing the decent_exposure gem. You can learn more about it here: http://railscasts.com/episodes/259-decent-exposure
Source: https://github.com/voxdolo/decent_exposure
It's a method from the Decent Exposure gem. You can check out a screencast that Ryan Bates did on it over at Railscasts. It's a really great gem. I use it in my application. It cuts down on a lot of the redundancy in the controller layer.
expose is not a part of Rails, it comes from the decent_exposure gem.
It is not an answer to the question. I just want to make the Rails world a bit better and I hope that somebody will read this.
Please think twice before using expose. You should only use it if you 100% sure you are using it the right way and it really makes the code better. Read the documentation properly!
One of projects I worked on became unmaintainable because of tons of expose in controllers which replaced not only all #instance_variables passed to views, but also a lot of business logic and the most helper methods.
When you use expose it is not clear in which controller actions and in which views it is used. Unexperienced developers combine data and logic for multiple actions and multiple views in the same expose block.
That's a nightmare.
Believe me, expose really destroys projects if not used properly.