List all sub categories in collection select - ruby-on-rails

Building a form for users to submit data. I can't seem to understand or figure out how *collection_select* method works.
Essentially I'm trying to give my users the option to choose which sub_category their product belongs in when submitting the form. What should the collection_select syntax look like?
I ended up figuring it out based on comments, here is what I ended up with, in case it helps anyone else.
<%= f.collection_select(:sub_category_id , SubCategory.find(:all), :id , :name) %>
Edited

SubCategory.all
instead of
Sub_category.all

Related

How do I create a drop down in a Rails form where the user can change the default value?

I'd like to have a drop down in my Rails form where users can select an area of a city, e.g. "Marchmont", "New Town", "Baberton" etc, when adding an order. I'd like that once they have made a selection, this will then be the default selection for the following times they use the form to add an order (so that they don't have to keep selecting it) but also that they can change their selection at any time. Hope that makes sense. I'm using the Simple Form gem. Thanks in advance! :)
#Steve
I will make a couple of assumptions.
1.) you know how to create forms within the rails templating engine.
2.) you understand how to create a dropdown menu using the Simple Form gem
So you have a couple of options based on what you actually want to accomplish. Based on what you are briefly describing, it sounds like you have some kind of an e-commerce/checkout situation that you want auto-completion to make it easier for a user.
there are a couple of approaches to storing this data.
Saving the user Data.
1.) Save it right on the user model under district_of_last_order
2.) Save it right on the order model that a user has_many orders. Then you can pull the first order's city district and select that
Personally I would lean on #2 as you probably want to be able to tightly couple the order with the user and saving that information twice is redundant since you can always do something like current_user.orders.first.district or whatever,
in your ERB where you build the form you can then do something along these lines:
<%= simple_form_for(#order) do |f| %>
... other input fields
<% if current_user.orders.first %>
<%= f.input as: :select selected: current_user.orders.first.district %>
<% else %>
<%= ... your regular dropdown menu here without the default %>
<% end %>
... rest of your form
If you have the option of using gems, I have had good results with select2-rails.

How can I use Simple Form in Rails to only show a specific set of nested attributes?

I have an has many association on my School model called PlayerScrape. I want to use the following code to only show a filtered collection of the association in my form. How can I do it given the collection #scraped_records being my filtered association records?
The following currently shows the 64 records that are associated with that School.
<%= form.simple_fields_for :player_scrapes do |field| %>
The collection, #scraped_records contains only 13 records of the association that I would like to show in my form.
You can use something like this i guess form.association :player_scrapes, collection: #scraped_records". If this is what you need there is more option for association method on their github documentation. Hope it helps.

Rails collection_select: using 'prompt' with logic - is there a better way?

So, I am using a form_tag to create my own search facility on a site displaying items.
I have a collection_select which lists locations of the items. I initially used prompt: 'n/a' to display 'n/a' without adding 'n/a' into the list of locations and then tried to use select: params[:search][:location] to keep the selection in the list after submitting. Basically I could get one of them to work at a time. Never both. I have written a work-around below:
<%= collection_select(:search, :location, Item::LOCATIONS, :to_s, :titleize, prompt: (#location ? params[:search][:location] : 'n/a')) %>
This is working but it feels wrong. I have obviously created a variable called #location in the controller for when the params exist and then added logic into the collection_select tag.
Any cleaner solutions to this would be appreciated. I am very new to Rails (and coding!) so trying to learn best practices.

Show last record in db table Rails .last method

I have 2 tables with a relation between them:
Users table: user_id,
Blogs table: user_id, blog_content,
Im working with a view that should show a users latest blog entry.. When I use
<%= #users.blogs.last %>
I get "#"
Can someone assist as to why its showing "#" and how to actually show the the last blog entry made by a #user?
Many thanks!!
I found out, I needed to define what part of the hash I needed to show
<%= #users.blogs.last[:blog_content] %>
I'd have thought you want to do:
<%= #users.blogs.last.blog_content %>
What you've done will try and display the Blog object itself with the to_s method, which returns something that looks like the following:
#<Blog:0x?????????>
Which on screen will just look like a hash.

Count value of records which belong to other model in rails

This is probably pretty basic, but I'm trying to figure out how to show how many comments a post has in rails, on the post index page of my app.
comments belongs_to post, and post has_many comments
Just not sure how to show on the index page, the amount of comments for each post.
Thanks in advance!
Elliot
Try this.
<%= post.comments.size %>
You might also be interested in the pluralize method.
<%= pluralize(post.comments.size, 'comment') %>
This will output "3 comments" or "1 comment", etc.
I may be wrong here, but you should use
<%= post.comments.count %> rather than size.
ActiveRecord knows that 'count' is a special method, and will turn it into a SELECT count(id) from comments where post_id = x (which is what you want).
size however, is not a special method, and ActiveRecord will load all the comments into an array in memory (SELECT * from comments where post_id = x, and then tell you how long this array is, which may be unneccessary - if you're going to loop through the array of comments further down the page, then you may want to use size to load them into memory, because it will need to happen later anyway.
You should also use some of ActiveRecord's built in functionality here. The counter_cache. Check it out here

Resources