Rails 3 - how to make ternary condition in view? - ruby

I have ternary operator and I am trying this ternary operator put into checkbox, but I am still making fault in writing (syntax error)...
So I would like to ask about help, how to do...
CAR: <%= f.check_box :car, :value => 2, ((f.sex == 2) ? (:checked => true) : (:checked => false)) %>

You don't need a ternary operator here. Try this instead:
CAR: <%= f.check_box :car, :value => 2, :checked => (f.sex == 2) %>
Also your problem comes from the fact that in a Hash literal you can't define keys conditionally, so:
{:a => (:b || :c)} is valid
{:b ? (a: => :b) : (:a => :c)} is invalid

<%= f.check_box :car, :value => 2, :checked => f.sex == 2 ? true : false %> will work, but can be shortened to <%= f.check_box :car, :value => 2, :checked => f.sex == 2 %>!

Related

Find keep duplicates in Ruby hashes

I have an array of hashes where I need to find and store matches based on one matching value between the hashes.
a = [{:id => 1, :name => "Jim", :email => "jim#jim.jim"},
{:id => 2, :name => "Paul", :email => "paul#paul.paul"},
{:id => 3, :name => "Tom", :email => "tom#tom.tom"},
{:id => 1, :name => "Jim", :email => "jim#jim.jim"},
{:id => 5, :name => "Tom", :email => "tom#tom.tom"},
{:id => 6, :name => "Jim", :email => "jim#jim.jim"}]
So I would want to return
b = [{:id => 1, :name => "Jim", :email => "jim#jim.jim"},
{:id => 3, :name => "Tom", :email => "tom#tom.tom"},
{:id => 5, :name => "Tom", :email => "tom#tom.tom"},
{:id => 6, :name => "Jim", :email => "jim#jim.jim"}]
Notes: I can sort the data (csv) by :name after the fact so they don't have to be nicely grouped, just accurate. Also it's not necessary two of the same, it could be 3 or 10 or more.
Also, the data is about 22,000 rows.
I tested this and it will do exactly what you want:
b = a.group_by { |h| h[:name] }.values.select { |a| a.size > 1 }.flatten
However, you might want to look at some of the intermediate objects produced in that calculation and see if those are more useful to you.

2 fields in thinking_sphinx search

I have two fields that I am passing into thinking_sphinx, one is a dropdown, the other a free text.
<%= select :search, params[:search], Category.joins(:posts).select('distinct categories.*').collect {|category| [ category.categoryname,category.categoryname ]}, :include_blank => 'Select a category...' %>
<%= text_field_tag :resume, params[:resume] %>
Its working with just the dropdown, but my syntax seems to be wrong to get the 2nd one to work.
#posts = Post.search :conditions=>{:search=>params[:search]},{:resume=>params[:resume]}
I'm getting : 3: syntax error, unexpected '\n', expecting tASSOC
'conditions' needs to be a hash, you have two hashes. Try this:
#posts = Post.search(:conditions => {:search => params[:search], :resume => params[:resume]})

type mismatch: String given - Trying to match strings in ruby

I am using authlogic-connect in Rails. I am using a simple haml template where i dont want to show the authorization providers who are already added.
%h2 My Account
%form.authentication_form{:action => connect_path, :method => :post}
%fieldset
%input{:type => :hidden, :name => :authentication_type, :value => :user}
%legend Add another Oauth or OpenID provider.
.oauth_providers
%ul
- %w(google facebook twitter yahoo).each do |name|
%li.oauth_provider
-unless "{user.authenticated_with}" =~ "{name}"
%img{:src => "/images/icons/#{name}.png"}
%input{:type => :radio, :id => "#{name}_oauth_provider", :name => :oauth_provider, :value => name}
.clearfix
%input.submit{:name => :submit, :type => :submit, :value => "Update"}/
I am encountering the error type mismatch: String. The user.authenticated with returns a string.
def authenticated_with
#authenticated_with ||= self.access_tokens.collect{|t| t.service_name.to_s}
end
What is the possible problem?
Stacktrace:
ActionView::Template::Error (type mismatch: String given):
7: %ul
8: - %w(google facebook twitter yahoo).each do |name|
9: %li.oauth_provider
10: -unless "{user.authenticated_with}" =~ "{name}"
11: %img{:src => "/images/icons/#{name}.png"}
12: %input{:type => :radio, :id => "#{name}_oauth_provider", :name => :oauth_provider, :value => name}
13: .clearfix
app/views/users/edit.html.haml:10:in `=~'
app/views/users/edit.html.haml:10:in `_app_views_users_edit_html_haml___2062853011_2171399360_0'
app/views/users/edit.html.haml:8:in `each'
app/views/users/edit.html.haml:8:in `_app_views_users_edit_html_haml___2062853011_2171399360_0'
Extracted source (around line #10): -- Shows error on line 10
Pass a regular expression as argument to String#=~:
>> string = "el"
>> "hello" =~ /#{string}/
=> 1

ruby array includes an id

I currently want to iterate over an array of Objects (2 properties: id & name) and check if the array contains a specific Id
How would I do this?
Enumerable#detect is ok, but I think that Enumerable#any? (which returns a boolean), is strictly what you asked for:
xs = [{:id => 1, :name => 'a'}, {:id => 2, :name => 'b'}]
puts xs.any? {|x| x[:id] == 1} # true
puts xs.any? {|x| x[:id] == 5} # false
Try detect
a = [{:id => 1, :name => 'a'}, {:id => 2, :name => 'b'}]
puts a.detect {|x| x[:id] == 1}

How can I get all the checked items from a submitted form with sinatra's params?

I'm running Sinatra 1.0 with HAML, my form has a number of checkboxes, for example books I like, and you would select all the books you want. The checkbox name is "books".
In sinatra params['books'] there should be an array of all the books that were checked, but it only has the last item that was checked, not an array.
How can I get all the checked items?
HAML:
%form{:action => "/test", :method => 'post'}
%input{:name=>'check',:type=>'checkbox',:value=>'item1'} item 1
%input{:name=>'check',:type=>'checkbox',:value=>'item2'} item 2
%input{:name=>'check',:type=>'checkbox',:value=>'item3'} item 3
%input{:type => "submit", :value => "send", :class => "button"}
Sinatra get method
post '/test' do
puts params['check'] #should be an array but is last item checked
end
Very close, but do not but numbers in the arrays
%form{:action => "/test", :method => 'post'}
%input{:name=>'check[]',:type=>'checkbox',:value=>'item1'} item 1
%input{:name=>'check[]',:type=>'checkbox',:value=>'item2'} item 2
%input{:name=>'check[]',:type=>'checkbox',:value=>'item3'} item 3
Now,
post '/test' do
puts params['check'] #puts an array of what was checked to stdout
end
Wouldn't that output a bunch of checkboxes with the same name? If so, params['check'] is probably getting replaced with each new checkbox.
Try naming each one something different. If you really want it in an array, try hacking the names:
%input{:name=>'check[1]',:type=>'checkbox',:value=>'item1'} item 1
%input{:name=>'check[2]',:type=>'checkbox',:value=>'item2'} item 2
%input{:name=>'check[3]',:type=>'checkbox',:value=>'item3'} item 3
Try
%input{:type => "checkbox", :value => "1", :name => "checkbox[]", :id => "id1"} Chk1
%input{:type => "checkbox", :value => "2", :name => "checkbox[]", :id => "id2"} Chk2
%input{:type => "checkbox", :value => "3", :name => "checkbox[]", :id => "id3"} Chk3
Then in the rails or sinatra
puts params[:checkbox]
Then you can see the checked items.

Resources