How to DRY up routes in Rails 3 - ruby-on-rails-3

In Rails 3, given routes like
get 'about/terms', :as => 'terms'
get 'about/privacy', :as => 'privacy'
get 'about/jobs', :as => 'career'
get 'about/feedback', :as => 'feedback'
get 'about/contact', :as => 'contact'
get 'about/us', :as => 'about'
How to DRY it up?

Recon something like this would do it:
['terms', 'privacy', 'jobs', 'feedback', 'contact' ,'us'].each { |r|
get "about/#{r}", :as => r
}

if about is a controller or you hava a controller for your static pages
['terms', 'privacy', 'jobs', 'feedback', 'contact' ,'us'].each { |r|
get "/#{r}", :controller => 'about', :action => r
}

Related

How to call helper method through select_tag

I am trying to call helper method from select_tag. I tried this:
<%= select_tag(:themes, options_for_select({"Black" => "compiled/styles", "Green" => "compiled/styles2"})) %>
When I put alert(this.options[this.selectedIndex].value) instead of calling method, it works. How can I change my code to call the method as desired?
EDIT
I have this on my application.js
$(document).ready(function() {
$('#display_themes').change(function(){
$.ajax({url: '<%= url_for :controller => 'application', :action => 'set_themes()', :id => 'this.value' %>',
data: 'selected=' + this.value,
dataType: 'script'
})
})
});
And in controller I have set_themes methode
def set_themes()
#themes = params[:id]
respond_to do |format|
redirect_to '/galleries'
format.html # index.html.erb
format.xml { render :xml => #themes }
end
end
The problem now is #themes still empty even I change the dropdown
Routes.rb
match '', :controller => 'application', :action => 'set_themes()'
To route to application#set_themes:
match '/set_themes', to: 'application#set_themes', as: :set_themes
With that done, just direct the url in ajax as follows:
$.ajax({url: "<%= set_themes_path %>",
It should be #themes = params[:selected] in your controller based on what you are trying to do in application.js.

elasticsearch geo_distance query give me error how to write geo_distance query?

I have a table, schools with the fields: id, name, address, city, state, zip, latitude and longitude.
I want to search for schools within 12km by giving latitude and longitude; I am using this query but it's not working.
curl -X GET "http://localhost:9200/schools/school/_search?=true" -d '{"filter" : {"geo_distance" : {"distance" :"12km", "location": "40,-70"}}}'
I get the following error:
{
"error":"SearchPhaseExecutionException[
Failed to execute phase [query], total failure;
shardFailures {[_na_][schools][0]: No active shards}{[_na_][schools][1]:
No active shards}{[_na_][schools][2]: No active shards}{[_na_][schools][4]:
No active shards}{[WJV55VsxQU-XW8VPmXImwA][schools][3]:
RemoteTransportException[[Archie Corrigan][inet[/192.168.1.109:9300]][search/phase/query]]; nested:
SearchParseException[[schools][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [
{\"filter\" :
{\"geo_distance\" :
{\"distance\" :\"12km\", \"location\": \"40,-70\"}
}
}]
]
]; nested: QueryParsingException[[schools] failed to find geo_point field [location]];}]",
"status":500
}
model configuration in rails
# ElasticSearch integration
include Tire::Model::Callbacks
include Tire::Model::Search
include Search::ReloadHelper
tire do
settings({
:analysis => {
:filter => Search::Filters.hs_name_filters,
:analyzer => Search::Analyzers.hs_name_analyzers
}
})
mapping do
indexes :id, :type => 'integer', :index => :not_analyzed
indexes :name, :type => 'string', :analyzer => :hs_name_analyzer
indexes :address, :type => 'string', :index => :not_analyzed
indexes :city, :type => 'string', :analyzer => :hs_name_analyzer
indexes :state, :type => 'string', :index => :not_analyzed
indexes :zip, :type => 'integer', :index => :not_analyzed
indexes :location, :type => 'geo_point', :lat_lon => true
end
end
def to_indexed_json
{
:id => id,
:name => name,
:address => address,
:city => city,
:state => state,
:zip => zip,
:location => {
:lat => latitude,
:lon => longitude
}
}.to_json
end
How can I get this to work?

Netzke basepack. Need advice with multi uploading fields

Is there any easy way to include the multiupload feature to NetzkeFormView or GridView(AddInForm)?
My current image uloading field with carrierwave is:
{:name => :image_link, :xtype => :displayfield, :display_only => true, :getter => lambda { |r| %Q(<a href='#{r.image.url}'>Download</a>) if r.image.url }},
{:name => :image, :field_label => "Upload image", :xtype => :fileuploadfield, :getter => lambda { |r| "" }, :display_only => true}

rails3.1 and formtastic 2.0.0.rc2 - undefined method `inputs'

I am using rails 3.1.0.rc3 with formtastic 2.0.0.rc2 and I am getting this error -
undefined method `inputs' for #<ActionView::Helpers::FormBuilder:0x000001059c2fb0>
Here is the block of code
= form_tag '#', :class => 'formtastic' do
= fields_for CustomFields::Field.new, :builder => Formtastic::Helpers::FormHelper.builder do |g|
= g.inputs :name => :attributes do
= g.input :_alias
= g.input :hint
= g.input :text_formatting, :as => 'select', :collection => options_for_text_formatting, :include_blank => false, :wrapper_html => { :style => 'display: none' }
= g.input :target, :as => 'select', :collection => options_for_association_target, :include_blank => false, :wrapper_html => { :style => 'display: none' }
Is this a bug ?
Thanks, Alex
You are trying to use a formtastic method here. When you are actually in a block for Rails's form builder.
You need semantic_form_for and formtastic in your Gemfile to use f.inputs for example..

How to translate find_by_sql statement into named_scope?

How do I translate the following into a named_scope?
def self.commentors(cutoff=0)
return User.find_by_sql("select users.*, count(*) as total_comments from users, comments
where (users.id = comments.user_id) and (comments.public_comment = 1) and (comments.aasm_state = 'posted') and (comments.talkboard_user_id is null)
group by users.id having total_comments > #{cutoff} order by total_comments desc")
end
Here's what I have right now but it doesn't seem to work:
named_scope :commentors, lambda { |count=0|
{ :select => "users.*, count(*) as total_comments",
:joins => :comments,
:conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil} },
:group => "users.id",
:having => "total_comments > #{count.to_i}",
:order => "total_comments desc"
}
}
Ulimately, this is what worked;
named_scope :commentors, lambda { |*args|
{ :select => 'users.*, count(*) as total_comments',
:joins => :comments,
:conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil} },
:group => 'users.id',
:having => ['total_comments > ?', args.first || 0],
:order => 'total_comments desc' }
}
The problem you have is because you replaced selecting from users, comments with an inner join with comments. To properly translate the SQL to a named_scope, use this:
named_scope :commentors, lambda { |cutoff|
{ :select => 'users.*, count(*) as total_comments',
:conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil } },
:from => 'users, comments',
:group => 'users.id',
:having => ['total_comments > ?', cutoff],
:order => 'total_comments desc' } }
And you can't have a default value for lambda parameters, so you can't use |cutoff = 0|.