I would like move the following from my View into the model's associated helper:
<%= link_to_unless params[:aged]=='0', "0", jobs_path(:aged => '0', :sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav]) %> |
<%= link_to_unless params[:aged]=='30', "30", jobs_path(:aged => '30', :sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav]) %> |
<%= link_to_unless params[:aged]=='60', "60", jobs_path(:aged => '60', :sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav]) %> |
<%= link_to_unless params[:aged]=='90', "90", jobs_path(:aged => '90', :sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav]) %>
I've tried this, but it causes an UNEXPECTED IDENTIFIER error (clearly I need to concatenate the results of the link_to_unless to the '|'):
link_to_unless params[:aged]=='0', "0", users_path(:aged=>'0',:sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav]) |
link_to_unless params[:aged]=='30', "30", users_path(:aged=>'30',:sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav]) |
link_to_unless params[:aged]=='60', "60", users_path(:aged=>'60',:sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav]) |
link_to_unless params[:aged]=='90', "90", users_path(:aged=>'90',:sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav])
It seems that I need to insert the results these helper methods into the HTML stream, but I'm not certain of the best approach.
Now that Rails 3 includes all helpers all the time (helpers :all) is there a way to instruct a model's view to only use the helper associated with the model? At this point, I'm adding the model's name into the name of the function--for example, 'jobs_sorted_column'.
** edit **
Refactored
jobs_helper:
def posted_filter(bucket)
link_to_unless params[:posted]==bucket, bucket, jobs_path(:posted =>bucket, :starting=>params[:starting],:sort=>params[:sort],:dir=>params[:dir])
end
view:
[ <% ['0','30','60','90'].each do |bucket| %>
<%= posted_filter(bucket) %> |
<% end %> ]
Issues:
Resulting output looks like [ 0 | 30 | 60 | 90 | ]. is there a simple fix to remove the 4th '|'?
It seems like there would be a more elegant way to pass the params to the route, including the one 'over-ridden' value (:posted=>bucket, in my example).
Try using collect and join, something like:
<%= ['0','30','60','90'].collect{ |x| "#{posted_filter(x)}" }.join(' | ') %>
See: Array #collect
You can do it even better:
def posted_filters(*args)
args.collect { |bucket|
link_to_unless(params[:posted]==bucket, bucket, jobs_path(:posted =>bucket, :starting=>params[:starting],:sort=>params[:sort],:dir=>params[:dir]))
}.join(' | ').html_safe
end
And in your view code:
[ <%= posted_filters(0, 30, 60, 90) %> ]
Related
Simple question. I am using Ruby V1.9.3 and I type;
<%= f_array = ['a','b','c'] %><br>
<%= f_array.join(' , ') %><br>
but they show up on the browser as;
["a", "b", "c"]
a,b,c
As far as I remember (until Ruby V1.8.3), it used to show up like;
abc
a,b,c
Did Ruby change their specification or did I miss something??
You error is here
<%= f_array = ['a','b','c'] %>
The statement <%= represents a print statement. Change it to
<% f_array = ['a','b','c'] %>
and the line will not be printed. ["a", "b", "c"] is the result of the inspection of an array.
2.1.5 :003 > puts ['a','b','c'].inspect
["a", "b", "c"]
For what it is worth, the code has another issue. Your view should not contain assignments. That's part of the business logic of your application.
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]})
I have some code in a view script that iterates through an array of arrays:
<% #rows.each do |data| %>
<%= data[0] %>: <%= data[1] %><br>
<% end %>
How can I easily convert each data array to a hash so that I can refer to each item with a key?
<%= data[:name] %>: <%= data[:email] %><br>
You can refer to the arrays with named values like this:
<% #rows.each do |name,email| %>
<%= name %>: <%= email %><br />
<% end %>
This assumes that every member of the #rows array will be the expected two-value array.
#Zach's answer is ok, but answering strictly what you asked for, it can be done this way:
#rows2 = #rows.map { |row| Hash[[:name, :email].zip(row)] }
#Zach and #tokland have supplied two fine answers. Sometimes it's nice to make first class data objects instead of relying on composition of primitive Hashes and Arrays. Struct is handy for this:
irb> EmailTuple = Struct.new :name, :email
=> EmailTuple
irb> rows = [%w{foo foo#example.com}, %w{bar bar#example.com}]
=> [["foo", "foo#example.com"], ["bar", "bar#example.com"]]
irb> rows2 = rows.map{ |row| EmailTuple[ *row ] }
=> [#<struct EmailTuple name="foo", email="foo#example.com">, #<struct EmailTuple name="bar", email="bar#example.com">]
irb> rows2.map{ |tuple| "#{tuple.name} has email #{tuple.email}" }
=> ["foo has email foo#example.com", "bar has email bar#example.com"]
Is there a one-line way to use include if the array it's searching may not be assigned?
I've tried a lot of variants of
(foo || []).include?(:bar)
but without success
If foo really is nil, as opposed to undefined, then (foo || []).include?(:bar) will do what you want, however if foo is not set to anything yet, then you will get a NameError so we can check for that with a longer oneliner...
defined?(foo) ? (foo || []).include?(:bar) : false
(foo ||= []).include?(:bar)
That will do the trick.
Maybe this?
foo.includes? :bar if foo
Since you are using partials, put this on the top of your partial:
<% foo = [] unless local_assigns.has_key?(:foo) # for Arrays %>
<% foo = {} unless local_assigns.has_key?(:foo) # for Hashes %>
<% foo = "" unless local_assigns.has_key?(:foo) # for Strings %>
etc.
This is the proper way of checking if a variable used in a partial has been set at all.
In the views using the foo partial:
<% render :partial => "foo", :locals {:a => 1, :foo => [2, 3, 4]} %>
<% render :partial => "foo", :locals {:a => 1} %>
In the first case, the foo variable will be [2,3,4] in the foo partial.
In the second case, the foo variable will not be put in local_assigns and will thus be given a default value as per the code above.
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 %>!