Add instruction to drop down select box in Rails? - html

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' %>

Related

Rails 5 Select tag inserts "[ ]", %5B %5D in html search string

I'm trying to use a rails select form field to populate multiple fields in a search form. But the search string returned has erroneous [] brackets inserted.
https://test-selects.c9users.io/cars?utf8=✓&car_year%5Bcar_year%5D=2016&commit=Search+Cars
proper format should be:
https://test-selects.c9users.io/cars?
utf8=✓&car_year=2016&commit=Search+Cars
<%= form_tag(cars_path, :method => "get", id:"search-form") do %>
<%= select(:car_year, :car_year, ["2017", "2016", "2015"]) %>
<%= submit_tag "Search Cars", class: "search-but" %>
<% end %>
The code above is a simplified version using just a few sample years but it yields the same results as when I was returning an array from my model that held the data for the select field.
select(:car_year, :car_year,...
I've tried every combination of :car, params[car_year] etc. and the brackets alway show up.
The same code works fine in a post method. When I provide a properly formatted search URL the search provides the expected results, I can also get expected results when I use a text_field_tag so I know my controller and model are working properly.
Help for this rookie would be greatly appreciated.
Code show above was simplified but produces the same results.

How to get value from select and use it in Rails

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

Rails/Sqlite 3 - Displaying table data on page

I'm very very very new to Rails, and I've got a fast approaching deadline to code my first rail app as part of a challenge.
I've so far created the basic blog application, to get my head around how rails works.
I need to display a list of all data held within a table on a page in my site. In blog, I've got an index page - which lists all blog posts so I've been trying to follow the same process.
I've created a populated table in my database, and a controller - but when I try and display info from that database on my page I get the following error:
uninitialized constant RecordsController::Aliens
class RecordsController < ApplicationController
def index
#records= Records.all
end
end
All I want to achieve is a simple table, which has the list of my records in, so far I'm not having much luck retrieving the data from the database.
Any help appreciated!
I assume you want to list all aliens am I right?
As your model is called Alien you can call it like that:
def index
#aliens = Alien.all
end
And then in your view something like:
<% #aliens.each do |alien| %>
<div class="alien">
<ul>
<% alien.attributes.each do |attr_name, attr_value| %>
<li><strong><%= attr_name %>:</strong> <%= attr_value %></li>
<% end %>
</ul>
</div>
<hr>
<% end %>

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) %>

Reconstruction User based Modular Website

I am developing a website in Ruby on Rails, MySQL, and Javascript.
The website is modular, meaning that a user can customize their homepage and drag and drop one module to another area where it will live. there are three columns where the module can be dropped. (Think iGoogle or Netvibes)
What is the best way to store this data, so that when the user returns their homepage can be reconstructed quickly?
I've considered doing it in a way where each module get's an ID that corresponds to the user, it's row and it's positiong in that row. (So something like.. user|column|row, would equal 1204|3|27, meaning that for user #1204 this module would be in column #3 and 27 spaces from the top.
Then when the user returns, just loop through, adding 1 to each until it reaches the end of the column and start again at the other one until all 3 columns are populated.
I feel like this would be very slow though, and there must be a better way to do it.
Any suggestions? Mostly looking for Database structure, but if you have some Rails code that would corrilate to this I wouldn't mind seein git.
I think the best is to keep your data in a single text field
For example 1204|3|27 should be in a text field ... with a good index per user id you should get the configuration very very fast. After that you just need to "explode" your configuration for "|" .
Regards
I say model it very straightforwardly:
class ModuleInstallation < AR::Base
belongs_to :user
belongs_to :module
validates_presence_of :column
validates_presence_of :row
end
class User < AR::Base
has_many :module_installations, :order => :column
has_many :modules, :through => :module_installations
end
Then let your controller handle any more sorting, something like this (untested, but look through it and the documentation for the methods I'm using to get the concepts):
#columns = current_user.module_installations.group_by(&:column)
#columns.each { |column, modules| modules.sort! { |x,y| x.row <=> y.row } }
Then your view is relatively simple:
<% #columns.each do |column, modules| %>
<div class="column-<%= column %>">
<% modules.each do |module| %>
<div class="module">
<%= module.to_html %>
</div>
<% end %>
</div>
<% end %>