Skip to content

Instantly share code, notes, and snippets.

@victorcreed
Last active December 27, 2015 18:09
Show Gist options
  • Save victorcreed/7367094 to your computer and use it in GitHub Desktop.
Save victorcreed/7367094 to your computer and use it in GitHub Desktop.
really lazy and dont want to create mess in VIEW, so i used splat to passing dynamic parameters
<%= simple_form_for @job, html: { class: "form-horizontal" } do |f| %>
<% new_and_edit_form_fields.each do |column| %>
<%= f.input *column %>
<% end %>
<% end %>
class Job < ActiveRecord::Base
belongs_to :user
def self.public_attributes
column_names.map(&:to_sym).reject{ |x| [:id, :updated_at, :created_at, :user_id].include?(x) }
end
attr_accessible *public_attributes
end
module JobsHelper
def new_and_edit_form_fields
Job.public_attributes.keep_if{ |x| ![:user_id, :status, :published].include?(x) }.map do |jp|
send("#{jp.to_s}_helper") rescue jp
end
end
def subject_helper
[:subject, {wrapper_html: { class: "form-holder" }, input_html: { cols: 5, rows: 3} }]
end
def grade_helper
[:grade, { collection: ["elemantry"], wrapper_html: { class: "form-holder" }}]
end
def covered_teacher_contact_information_helper
[:covered_teacher_contact_information, { wraper_html: { class: "form-holder" }}]
end
end
@mrrazahussain
Copy link

Its very useful

thanks for it.

@victorcreed
Copy link
Author

thanks

@muaazrafi
Copy link

Great Thought , one little tweak to the JobsHelper

forms = {
subject: [:subject, {wrap_html: { class: "form-holder" }, input_html: { cols: 5, rows: 3} }],
grade: [:grade, { collection: ["elemantry"], wrap_html: { class: "form-holder" }}]
covered_teacher_contact_information: [:covered_teacher_contact_information, { wrap_html: { class: "form-holder" }}]
}

forms.each do |key,value|
define_method("#{key}_helper") { value }
end

That should reduce code.

@victorcreed
Copy link
Author

ye thanks @muaazrafi its seems enhanced version, ill try.

@SyedRaza
Copy link

SyedRaza commented Nov 8, 2013

Good Work..
Nice innovation would give it a try..
Thanks for sharing Experience..

@bilal-ahmad
Copy link

pretty handy, good job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment