Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Make yourself the default (extra credit)

Hey guys,

I've been going back over my work on the "Build a Simple Ruby on Rails Application" to also include the extra credit. I didn't know there was extra credit until I stumbled upon it just recently, so I thought it would be fun.

However, I'm stuck on the part in "Customizing Forms" related to "Make yourself the default."

I was able to order the drop down list easy enough using collection: User.order(:first_name). However, I can't figure out how to select the default user.

Here's the code I've come up with after going back and forth:

<%= f.input :user, collection: User.order(:first_name), label_method: :full_name, include_blank: false, selected: current_user.full_name %>

There is no error created in this instance, however the list does not default to the current user but to whichever user is listed first (in my case "Evan"). I checked to see if it is in fact dependent on which user is first by reversing the order and I was correct.

I've been jumping around online to find a solution and some people recommend working with the controllers and also using Ruby's helpers. While I have a little bit of an idea of how to manipulate those, I figured I'd just bite the bullet and ask for help here since everything I've learned from Ruby now has been from TeamTreehouse.

Thank you in advance!

9 Answers

Got it! It's actually much simpler than what I described above. There are values already coming from current_user (current_user.id). So just use that in selected: and you're good to go.

Caleb, you're a genius!

Upon inspecting the form page (when the rails server is running) there were values already assigned to each user (in order of creation "1, 2, 3, etc."). I didn't really think anything of this until you said "selected: seems to only accept an integer." This made me remember that in the simple_form documentation there was a section in "collection" that described value_method.

I didn't see the value (no pun intended) of this, but instead of using a numerical numbering system, you can instead use the :full_name as a value system and then set selected: to "current_user.full_name"

Wham bam! Solved! Thank you so much for being a sounding board and sticking with me on this! You're awesome! :-)

Maybe try something like :default => current_user ?

I tried that too. Same problem (still defaults to the user at the top of the list). :-/

I know I'm late, but I figured it out! There's an option called "selected."

<%= f.input :user_id, collection: User.order(:first_name), selected: current_user, label_method: :full_name %>

Hey, better late than never! :-)

Yeah, I tried that too, as you can see in the code I posted above. It didn't work for me at all (tried a few different ways with this option and it wouldn't change the default at all). I wondered if it had to do with

User.order(:first_name)

since that may force it into an order by first name regardless of which item was selected. So I changed the collection to "User.all" but still no luck.

It kills me to move on from this problem since I figured if I just kept going I might be able to learn a solution or deduce where I went wrong, but sadly I'm still stumped.

Try this! I've read around various places that "include_blank: false" breaks the "selected" thingy (I'm not sure what type of code "selected" is at this point in my adventure, so I went with 'thingy' to avoid talking out my ass).

Anyway, In my code, I left "include_blank" out completely and it works. There isn't even a blank line! My intuition is that it's unnecessary when there aren't any blank values in the database to place there. The blank line must come from simple_form as the default "default" item.

Man, even that didn't work! I'm curious if the fact that User.order() doesn't work how I would like it has anything to do with it. I can sort by either :first_name or :last_name, but both cases yield the same result (sorting by first name). I don't think this has anything to do with my problem, but the fact that the data isn't working like how I would expect is strange. Especially since I followed the tuts to the letter.

I took a look at your github page, Caleb, and downloaded a copy of your treebook app just to compare code and see if I missed something. I tried testing your code to see if yours worked differently from mine, but I experienced the same problem I'm having (won't default to current user, but instead sorts by first name since order was set to ":first_name"). Are you sure it worked as you expected? If so, then I must be missing something completely.

In any case, I hope to find out more as I become more proficient with Ruby because I definitely need more practice. Regardless of how frustrating this setback is, it's still loads of fun to work in RoR! :-)

You're right. I didn't realize it was showing the user at the top of the list, and not the current_user. I haven't fixed it, but I think I know what the problem is. selected: seems to only accept an integer, but the devise database doesn't have any kind of user_id integer that I know of. And we can't use the user_id from the statuses page, because it's not part of the same table or view or controller or something. If you manually change selected: to a number that identifies the position in the list, it works fine. Just need to find a way to pass a number from current_user. Any ideas?

Haha! Spoke too soon. Just found out it breaks the submission process since it returns a nil value. Still working on it...