How to get value from select and use it in Rails - html

I'm starting a little project in rails and I'm trying to make something like that:
<%= f.label :Zgłoszenie_do_działu %>
<%= f.collection_select :dzial_id, Dzial.all, :id, :nazwa, prompt: "Wybierz" %>
<%= f.label :id_pracownik %><br/>
<%= f.collection_select :pracownikid, User.find_by_dzial_id(:dzial_id), :nazwa, prompt:"Wybierz" %>
I want to get ID of "dzial" from the first select and use it to display in the second select IDs or names of users, that belong to choosen "dzial". Like you can see I was trying to make something, but anything worked for me. If it isn't possible I would be thankful, if anybody would show me how to deal with it.

If I do understand you properly, you have the following usecase: On your page you select a dzial and after that the second select field has to show all it's User ids.
Without refreshing the page
If you want to do this without refreshing the page, you can't really do it the 'rails way'. You could use for example jQuery to do an asynchronous call on change of the first select field. Then you can use the result of that call to update the second select field.
With refreshing the page
If you want to do this by only using ruby-on-rails you could trigger a refresh on the change of the first select or simply create a submit button. Then you can set the dzial_id parameter and use it to fill the second select field like you are already doing in your example.
Edit
I forgot to mention how to get the value from the select field.
By using jQuery, you could use $('#dzial_select').val()
By refreshing the page, you might want to create two seperate forms

Related

Hide a Post independently if current_user has marked Post as 'hidden'

I've been at this for days now without any luck. but I'm trying to allow users to hide or mark Posts as read independently. But I simply cannot get it to work. Maybe it's because my brain is currently inside-out.
Here's my query:
<%
#posts.includes(:postsettings).where.not(
postsettings: {user_id: current_user.id}).where(postsettings:
{delete_post: true}).each do |post|
%>
What I'm trying to do here is Exclude an entire Post IF any of the postsettings includes the current_user.id – But as soon as another user also marks it as deleted – It pops back up again for current_user because the criteria is met reversibly.
Everything else is set up correctly to the best of my knowledge, using a join table.
Any help would be greatly appreciated. What would be the correct approach?
I think this should work, I dont get why you're using two where queries on for relating same object.
<% #posts.includes(:postsettings).where.not(
postsettings: {user_id: current_user.id, delete_post: true}).each do |post| %>
.....
<% end %>
Hope this helps

How to show all the Users who have liked my specific Items

I apologize for the newbie questions still relatively new to rails. I'm trying to show all the users who have liked a current user's specific items. With the help of the SO community and looking at different Rails guides - I'm 85% there. Currently, I'm displaying all users who have liked all items (not just my specific ones which is what I want) I've listed below all the relevant simple code - thank you so much guys!!
Index.html.erb
<%- #likers.each do |liker| %>
<%= image_tag liker.avatar, width: 25, class: "css-style" %>&nbsp
<%= liker.username %>
<% end %>
Items_controller
def index
#items = Item.order("created_at DESC")
if current_user.present?
#likers = Item.where("user_id", current_user.id).map(&:users).flatten
end
end
So, you want all #likers to be all the people who like the current_user's items?
#likers = current_user.items.map(&:likes).flatten.map(&:user).flatten.uniq
I've added the uniq so if a user likes more than one of these posts, you won't see them turn up multiple times. You can omit that, of course, if you want duplicates.
I'm also making some guesses about your other models based on your previous question, so you might need to tweak it depending on your actual implementation.

Rails form_for setting options and setting selected option

Another simple question i'm sure so sorry in advance but i cant seem to find a solution to what i am trying to do.
I have form with a select field (vehicle_size) that i want to populate with all existing options that are currently in the database for records that have vehicle_size set. I also want to set the selected option to whatever the option in the database is currently set to (if set) but i guess the problem that i am having is the reason that this value isn't being set already.
I currently have this code:
<%= f.select(:vehicle_size, #models.index_by {|m| m.vehicle_size}, :selected => :vehicle_size) %>
which is populating the select options correctly but in my new and edit pages (both using this form) the select menu is blank even though currently all records are be set to one of the existing options (vehicle_size may actually be blank for some records in future).
If i then select an option from the menu and save the record the vehicle_size option has a strange value. something along the lines of :
#<Model:0x007fd13e6c3790>
I'm pretty sure i'm using index_by in the wrong way so some pointers on what i'm missing would be great.
The parameters of f.select are: attribute_name,option_tags,options.
option_tags is supposed to be a string of html option tags.
There are some helpers to construct such a string from i.e. an array of values, a collection etc.: options_for_select, options_from_selection_for_select.
You have to use one of these helpers.
You can get the existing vehicle_sizees with:
Model.pluck(:vehicle_size).uniq
so your select could look like:
<%= f.select :vehicle_size, options_for_select(Model.pluck(:vehicle_size).uniq,#model.vehicle_size) %>
See the Rails Guide on form helpers.

Add instruction to drop down select box in Rails?

I am displaying a list of all users (from a Users model) in my Rails 3 application. In a helper (in this case it's Faults) I have the following:
def user_all_select_options
User.all.map{ |user| [user.name, user.id] }
end
In the view I have the following:
<%= f.select :user_id, user_all_select_options %>
This simply creates a drop down list with the first users name pre-selected as it is at the top of the list. What I am trying to do is add an instruction(?) at the top of the list that is unselectable. The idea is to prevent the top user from getting constantly accidentally set as the required user even when they don't want to be.
So rather than the select box looking like this:
I would like it to look like this:
Is this possible, if so, how??
Pass the :prompt option.
<%= f.select :user_id, user_all_select_options, :prompt => 'Select a user' %>

Select Box not filling properly in rails

I am creating a select box for a form using this in _form.html.erb
<%= f.select(:category_id,options_for_select(#cats)) %>
#cats is an array created in my controller like this:
#cats = []
categories.each do |c|
#cats.push([c.full_name,c.id])
end
The select box is properly filled, and the selected foreign key is even properly saved to the database. The problem is, when I come back in my edit action, the select box is moved back to the first item in the list, not the one corresponding to category_id. Reading the documentation it seems like this should just magically work. How do I get it to select the proper value?
When you use the select helper you just pass the choices not the full option tags like you would with the select_tag helper. Try this instead
<%= f.select(:category_id, #cats) %>