Skip to content

Instantly share code, notes, and snippets.

documents = Document.find :all
format.xml{render :xml=>documents.to_xml(:include=>{:person=>{:only=>[:surname, :age]}, //how to rename the attribute "surname" to "name"?
:only=>:nr)}
template = 'people' # depends on the template
...
@nested_object = self.instance_variable_get(:"@#{params[:controller].singularize}") if show_action?
...
attributes.each do |attribute|
route = @nested_object ? self.send("#{params[:controller].singularize}_#{template}_path", @nested_object, {:order=>order}) : self.send("#{params[:controller]}_path", {:order=>order})
link_to('Fooo', route)
end
# towns_controller.rb
def show
@town = Town.find_by_id params[:id], :include=>:town
if_object @town do
@result_list = result_list_of :person, 'middle_age'
@nested_resource = :town
@people = Person.paginate :per_page=>@result_list.items_per_page,
:page=>@result_list.page,
:include=>Person.reflections.keys,
:order=>Person.sort_clause(@result_list.attribute_result_list),
<%form_for(@main_group, :url=>main_group_path(@main_group), :html=>{:id=>"form_panel"}) do |form|%>
<%=form.select(:group_ids, @groups.collect{|g| [g.code, g.id]}, {},
{:size=>4, :multiple=>true, :id=>'source_baugruppe_ids'})%>
<%=form.submit '>>', {:class=>'btn_shift_right'}%>
<%=form.submit '<<', {:class=>'btn_shift_left'}%>
<%=form.select(:group_ids, @main_group.groups.collect{|g| [g.code, g.id]}, {}, {:size=>4, :multiple=>true, :class=>'destination'})%>
<%-end-%>
people_controller.rb:
class PeopleController < ApplicationController
end
the PeopleController shall handle all people (asia, europe, america, africa, australia):
but i'd like to have routes like:
- /asia/12 -> PeopleController.show params=>{:id=>12, :continent=>'asia'} :get
- /america -> PeopleController.index params=>{:continent=>'america'} :get
- /africa/3/edit -> PeopleController.edit params=>{:id=>3, :continent=>'africa'} :get
map.people '/:person', :controller=>:people,
:requirements=>{:person=>/(asian|european|american|african|australian)/}
i expected: people_path(:person=>'asian') to create /asian but it creates /people?person=asian
map.with_options :controller=>'people' do |people|
people.people '/:continent', :action=>"index", :conditions=>{:method=>'get'},
:requirements=>{:continent=>/(asia|europe|america|africa|australia)/}
end
def destroy
callback(:before_destroy)
update_attribute_with_validation_skipping :inaktiv, true # i'd like to skip the after_update callback raised by update_attribute_with_validation_skipping
freeze
callback(:after_destroy)
end
Form-model form.rb:
before_destroy :validate_assoc
def validate_assoc
if inactive && !(towers = Tower.find(:all, :conditions=>{:form_id=>id})).blank?
towers.each{|tower| errors.add(:inactive, tower )}
false
end
end
string = File.open('my_file.csv'){|f| f.read}
p string
string.split("\n").each do |row|
row.strip.split("|").each do |col|
p(col)
end
end