John Do wrote:
Controllers/files_controller.rb class FilesController < ApplicationController def export_database(obj) obj.save_to_file end end
Views/files/show.html.erb <% [RmEnv, RmProc].each do |db_obj| %> <%= button_to "export database #{db_obj}", :action => export_database, # don't know how to pass parameter such that :obj => db_obj %> <% end %>
Or any other suggestion on who to activate a method via user activity.
Generally you aren't going to pass an entire object from the client to the server. Rather you will be passing an id of some sort, then doing a find, then performing an action. Without knowing what your RmEnv and RmProc objects look like, here is a guess:
Controllers/files_controller.rb class FilesController < ApplicationController def export_database obj = SomeClass.find_by_id(params[:id]) obj.save_to_file end end
Views/files/show.html.erb <% [RmEnv, RmProc].each do |db_obj| %> <%= button_to "export database #{db_obj.name}", :action => :export_database, :id => db_obj.id %> <% end %>
You very well may need to tweak this based on your application. If RmEnv and RmProc are of different classes, then you will also need to pass from the client which one you are wanting to export. I haven't tried this, but it should work:
button_to "Export ...", :action => :export_database, :id => db_obj.id, :class_name => db_obj.class.name %>
then in the controller
obj = (params[:class_name].constantize).find_by_id(params[:id})
which will take the string form of the class name and try to find a class (constant) defined for it. It will then call the find method on that class. You should add some error handling.
Peace.