Is it possible to either extract the text portion of a select tag after it is passed to a controller or do something else crafty with rails to get the value between the <option></option> tags?
My raw html is simple:
<select>
<option value></option>
<option value="5">I have a small problem</option>
<option value="10">I have a big problem</option>
<option value="15">I have a massive problem</option
</select>
I'm converting the selected value to integers in my controller which triggers other system calls (such as routing to the correct support person), however, I'd like to be able to also save the text portion into the user's profile for tracking (i.e. `user.issue = "I have a small problem").
Shy of creating some hidden fields and using Javascript, is there another way to get or include the text value when it passes to the controller?
Here is some advice which is going to be very general since you don't give a lot of specifics in your question. Yes, the HTML should be "the same" no matter what but really it does matter how you create it because that reflects how it integrates into the rest of your app.
I would create a table in the DB called severity with rows something like this:
id | text | val
1 | 'small' | 5
2 | 'big' | 10
3 | 'massive' | 15
Now instead of doing translations elsewhere like you imply ("I'm converting the selected value to integers in my controller which triggers other system calls") you can use things like:
problem.severity.text
=> 'massive'
problem.severity.val
=> '15'
Using relationships like has_one, belongs_to, etc. you can then leverage these values or words anywhere in your application. Something as simple as:
<%= f.collection_radio_buttons :user_id, Severity.all, :id, :text %>
I should recommend you to use select2 for multiple values selecting, select2-rails is its integration with rails. It's pretty simple. Give it a shot.
Related
So I am new to Rails, and OO programming in general. I have some grasp of the MVC architecture. My goal is to make a (nearly) completely dynamic plug-and-play plotting web server. I am fairly confused with params, forms, and select helpers.
What I want to do is use Rails drop downs to basically pass parameters as strings to my controller, which will use the params to select certain column data from my database and plot it dynamically. I have the latter part of the task working, but I can't seem to pass values from my view to controller.
For simplicity's sake, say my database schema looks like this:
--------------Plot---------------
|____x____|____y1____|____y2____|
| 1 | 1 | 1 |
| 2 | 2 | 4 |
| 3 | 3 | 9 |
| 4 | 4 | 16 |
| 5 | 5 | 25 |
...
and in my Model, I have dynamic selector scopes that will let me select just certain columns of data:
in Plot.rb
class Plot < ActiveRecord::Base
scope :select_var, lambda {|varname| select(varname)}
scope :between_x, lambda {|x1,x2| where("x BETWEEN ? and ?","#{x1}","#{x2}")}
So this way, I can call:
irb>>#p1 = Plot.select_var(['x','y1']).between_x(1,3)
and get in return a class where #p1.x and #p1.y1 are my only attributes, only for values between x=1 to x=4, which I dynamically plot.
I want to start off in a view (plot/index), where I can dynamically select which variable names (table column names), and which rows from the database to fetch and plot. The problem is, most select helpers don't seem to work with columns in the database, only rows.
So to select columns, I first get an array of column names that exist in my database with a function I wrote.
Plots Controller
def index
d=Plot.first
#tags = d.list_vars
end
So #tags = ['x','y1','y2']
Then in my plot/index.html.erb I try to use a drop down to select wich variables I send back to the controller.
index.html.erb
<%= select_tag( :variable, options_for_select(#plots.first.list_vars,:name,:multiple=>:true) )%>
<%= button_to 'Plot now!', :controller =>"plots/plot_vars", :variable => params[:variable]%>
Finally, in the controller again
Plots controller
...
def plot_vars
#plot_data=Plot.select_vars([params[:variable]])
end
The problem is everytime I try this (or one of a hundred variations thereof), the params[:variable] is nill.
How can I use a drop down to pass a parameter with string variable names to the controller?
Sorry its so long, I have been struggling with this for about a month now. :-( I think my biggest problem is that this setup doesn't really match the Rails architecture. I don't have "users" and "articles" as individual entities. I really have a data structure, not a data object. Trying to work with the structure in terms of data object speak is not necessarily the easiest thing to do I think.
For background: My actual database has about 250 columns and a couple million rows, and they get changed and modified from time to time. I know I can make the database smarter, but its not worth it on my end. I work at a scientific institute where there are a ton of projects with databases just like this. Each one has a web developer that spends months setting up a web interface and their own janky plotting setups. I want to make this completely dynamic, as a plug-and-play solution so all you have to do is specify your database connection, and this rails setup will automatically show and plot which data you want in it. I am more of a sequential programmer and number cruncher, as are many people here. I think this project could be very helpful in the end, but its difficult to figure out for me right now.
I'd like to be able to add "meta" information to a model, basically user-defined fields. So, for instance, let's imagine a User model:
I define fields for first name, last name, age, gender.
I would like users to be able to define some "meta information", basically to go in their profile page and share other information. So one user might want to add "hobbies", "occupation", and "hometown", and another might want to add "hobbies", and "education".
So, I'd like to be able to have a standard view for this kind of stuff, so for instance in the view I might do something like (in HAML):
- for item in #meta
%li
%strong= item.key + ":"
= item.value
This way I can ensure that the information is consistently displayed, rather than just providing a user with a markdown textbox that they may format all different ways.
I'd also love to be able to click on meta and see other users who have given the same thing, so in the example above both users defined "hobbies", it would be nice to be able to say I want to see users who have shared hobbies -- or even better I want to see users whose hobbies are ___.
So, since I don't know what fields users will want to define in advance, what kind of options are there for providing that kind of functionality?
Is there a gem that handles custom meta information on a model like this, or at least sort of similarly? Has anyone had experience with this kind of problem? If so, how did you solve it?
Thanks!
The dynamic field implementation depends upon following factors:
Ability to dynamically add attributes
Ability to support new data types
Ability to retrieve the dynamic attributes without additional query
Ability to access dynamic attributes like regular attributes
Ability query the objects based on dynamic attributes. (eg: find the users with
skiing hobbies)
Typically, a solution doesn't address all the requirements. Mike's solution addresses 1, and 5 elegantly. You should use his solution if 1 & 5 are important for you.
Here is a long solution that addresses 1,2,3, 4 and 5
Update the users table
Add a text field called meta to the users table.
Update your User model
class User < ActiveRecord::Base
serialize :meta, Hash
def after_initialize
self.meta ||= {} if new_record?
end
end
Adding a new meta field
u = User.first
u.meta[:hobbies] = "skiing"
u.save
Accessing a meta field
puts "hobbies=#{u.meta[:hobbies]}"
Iterating the meta fields
u.meta.each do |k, v|
puts "#{k}=#{v}"
end
To address the 5th requirement you need to use Solr Or Sphinx full text search engines. They are efficient than relying on DB for LIKE queries.
Here is one approach if you use Solr through Sunspot gem.
class User
searchable do
integer(:user_id, :using => :id)
meta.each do |key, value|
t = solr_type(value)
send(t, key.to_sym) {value} if t
end
end
def solr_type(value)
return nil if value.nil?
return :integer if value.is_a?(Fixnum)
return :float if value.is_a?(Float)
return :string if value.is_a?(String)
return :date if value.is_a?(Date)
return :time if value.is_a?(Time)
end
def similar_users(*args)
keys = args.empty? ? meta.keys : [args].flatten.compact
User.search do
without(:user_id, id)
any_of do
keys.each do |key|
value = meta[key]
with(key, value) if value
end
and
end
end
end
Looking up similar users
u = User.first
u.similar_users # matching any one of the meta fields
u.similar_users :hobbies # with matching hobbies
u.similar_users :hobbies, :city # with matching hobbies or the same city
The performance gain here is significant.
If each user is allowed to define their own attributes, one option might be to have a table with three columns: user_id, attribute_name, attribute_value. It might look like:
| user_id | attribute_name | attribute_value |
| 2 | hobbies | skiing |
| 2 | hobbies | running |
| 2 | pets | dog |
| 3 | hobbies | skiing |
| 3 | colours | green |
This table would be used for finding other users who have the same hobbies/pets/etc.
For performance reasons (this table is going to get large) you may want to maintain multiple places that the info is stored -- different sources of info for different purposes. I don't think it's bad to store the same info in multiple tables if absolutely necessary for performance.
It all depends on what functionality you need. Maybe it will end up making sense that each user has their key/value pairs serialized into a string column on the users table (Rails provides nice support for this type of serialization), so when you display info for a particular user you don't even need to touch the huge table. Or maybe you will end up having another table that looks like this:
| user_id | keys | values |
| 2 | hobbies, pets | skiing, running, dog |
| 3 | hobbies, colours | skiing, green |
This table would be useful if you need to find all users that have hobbies (run LIKE sql against the keys column), or all users that have anything to do with a dog (run LIKE sql against the values column).
That's the best answer I can give with the requirements you gave. Maybe there is a third-party solution available, but I'm skeptical. It's not really a "pop in a gem" type of problem.
In this case, I would at least consider a documentdb like mongo or couch, which can deal with this type of scenario much easier then an rdms.
If that isn't the case, I would probably end up doing something along the lines of what Mike A. described.
This SHOULD be trivially easy, but doesn't seem to be in Rails...
we need to gather text input from the user of no more than 200 chars.
if we use text_field, and set :maxlength to 200, we seem to have only two equally UGLY choices: (a) make the field soooooo wide using :size that it won't fit on a typical user's screen OR (b) the user has to scroll horizontally while typing. Either approach stinks from a UI point of view.
what we WANT of course is to use text_area, and have it automatically limit the input to 200... but of course text_area has NO limit.
we certainly don't want to wait until they SUBMIT the data to tell them "that was too long" or even worse simply truncate their data.
any cool ideas? How do we have an input box say 100 chars wide and 3 lines tall that will not let the user type more than 200 characters?
Is there some UI-enhancement framework that handles this sort of UI issue? Is adding a javascript counter in such a way that we apply it ONLY to certain input fields easy?
Try this : = f.text_field :phone_area, :size => 4, :maxlength => 3
You can limit number of characters in textbox using javascript.
You can, for example set a :style => "limit" or :"data-limit"=> 200 and then use css selectors in jQuery to target only those textboxes.
Something like:
$(".limit").maxlength({
// options
})
Or even neater:
var limited_textbox = $("[data-limit]");
limited_textbox.maxlength({
maxCharacters: limited_textbox.data("limit"),
// options
})
With a plugin like http://www.stjerneman.com/demo/maxlength-with-jquery
or similar http://plugins.jquery.com/plugin-tags/character-counter
As for UI, you could use facebook like textbox: http://www.9lessons.info/2010/03/facebook-like-expanding-textbox-with.html
Is there any better approach than what I am currently doing here:
MVC Controller action creates a select list as:
ProductsDDL.Select(rp => new SelectListItem
{ Value = Model.RawMaterialID.ToString() + "," + plant, Text = Model.FinishedProductName });
And HTML rendered as:
<select id="Products" name="Products">
<option value="3,PLANT1">Finished Product1</option>
<option value="4,PLANT2">Finished Product2</option>
<option value="7,PLANT3">Finished Product3</option>
</select>
On selection change, I use Jquery $.GetJSON to populate another drop down list. The reason I am concatinating PlantID with RawMaterialID is to avoid long query processing time.
On Post to action(string RawIDPlantID), I use Split(',') to get RewMaterialID & PlantID
Other options are to use session to hold PlantID or input hidden field in MVC view.
I typically stay away from using commas as a delimiter in values that represent a single entity, and save the commas delimiting a list of multiple ids. In cases such as yours, I end up using an underscore. Then, should I need to comma separate a list of the IDs, it's easier on the eyes and to parse. But that's completely subjective and up to you, as the developer.
For example
3_PLANT1,4_PLANT2,7_PLANT3
is easier to eyeball
3,PLANT1,4,PLANT2,7,PLANT3
And that was as much criticism as I could muster about your code. The rest of it I'd use, and have used in the past.
I have a standard master-detail relationship between two models in a RoR application. The detail records contain four boolean fields indicating presence / absence of something.
When I display the detail records I want to add a summary indicating the number of records which have their boolean value set to True for each of the four boolean fields.
For example:
Date | Boolean Field 1 | Boolean Field 2 | etc
2009/08/29 | T | T |
2009/08/30 | T | F |
2009/08/31 | F | T |
2009/09/01 | F | T |
Total: 4 2 3
I tried using something like #entries.count(["Boolean Field 1", true])
The way I see it, there are two ways to calculate these values: one at the model by executing an SQL query (ugly) or at the view level by using a counter (ugly again.) Is there some other way to achieve what I want?
Thank you for your time,
Angelos Arampatzis
May be
#entries.select {|r| r.bool_field1}.size
You can either do:
#entries.count(:conditions => { :boolean_field_1 => true })
You can pretty this up by doing a named scope:
named_scope :booleans, :conditions => { :boolean_field_1 => true })
and then
#entries.booleans.count
Or if you already have ALL the items in an array (rather than a select few) and do not want to hit the database…
Rails provides a ? method for all columns. So while you have:
#entry.boolean_field
You also have:
#entry.boolean_field?
So you can do this:
#entries.collect(&:boolean_field?).length
sql isn't as ugly as rails makes it out to be and it is rather efficient, just make it a named_scope and your controller/view will still look pretty
Because you have all your entries as Rails objects, you can use the shortest form:
#entries.count(&:boolean_field1?)
It's using Enumerable#count.
Keep in mind though, that it counts using Ruby (as opposed to SQL). If you'll ever want to count without reading all records from DB, you will need to use something different for efficiency.