Skip to content

Instantly share code, notes, and snippets.

View matatabi's full-sized avatar

matatabi matatabi

View GitHub Profile
@matatabi
matatabi / form.rb
Created July 4, 2011 04:01
multiple checkboxes
serialize :product_categories
<%= select_product_categories(@vendor.registration_request.product_categories) %>
def select_product_categories(array)
if array == nil or array.length == 0
array = []
end
@matatabi
matatabi / sort.rb
Created May 11, 2011 12:37
sorting
@users.sort! { |a,b| a.name.downcase <=> b.name.downcase }
@matatabi
matatabi / character_count.js
Created March 14, 2011 14:55
jQuery と rails 色々
// require this file in the header
// And also add the following to the bottom of whatever partial you're using
// <script type="text/javascript">
// $("#pl_<%= index %>_full").charCount({counterText: '文字数:', transCost: 15, language: '#locale_<%= index %>'});
// </script>
(function($) {
$.fn.charCount = function(options){
@matatabi
matatabi / output_to_erb.rb
Created March 10, 2011 19:26
Output erb template to a variable
erb = ERB.new("<%= @vendor.email %>")
@mytext = erb.result(binding)
@matatabi
matatabi / class.phpmailer.php
Created March 7, 2011 18:08
modx で sendmail 使わずに google apps に smtp する方法
/* SMTP variables を設定
http://forum.whmcs.com/showthread.php?t=13845
↑の設定のほぼパクリ。ただ、もとネタのように、 "tls://" を tls のトリガーに使ってしまうと、コロンのせいで explode されてしまう。(e.g. "smtp1.example.com:25;smtp2.example.com")のフォーマットに対応するため。。。
なので、 tls// のみをトリガーにする。後の strpos 部分と一致してれば、トリガーは何の文字列でもOK。
*/
/////////////////////////////////////////////////
// SMTP VARIABLES
/////////////////////////////////////////////////
var $Host = "tls//smtp.gmail.com"; // the "tls//" is used later to trigger tls
@matatabi
matatabi / ajax_forms.html
Created February 22, 2011 08:55
Trigger rails 3 form submit without using the submit button
<script type="text/javascript">
function send(){
// check "send_now" and submit using ajax
// Assumes you're using jquery.rails.js
$("#send_now").val("true");
$("#f").trigger("submit");
};
</script>
<input type='hidden' id='send_now' name='send_now' />
@matatabi
matatabi / forms.html.erb
Created February 16, 2011 10:33
examples for forms
Different ways in which you can use the form_for helper.
<%= form_for([:admin, @order], :html => {:multipart => true, :class => "std", :name=>'f' }) do |f| %>
<%= form_for(@order, :url=> somepath, :html => {:method => :put, :class => "std", :name=>'f' }) do |f| %>
<%= form_for("order[something]", :url=> somepath, :html => {:method => :put, :class => "std", :name=>'f' }) do |f| %>
sql = "SELECT DISTINCT p.id, p.name as original_name, p.thumbnail_file_name, p.sku,
pv.vendor_id, pv.price, pv.stock, pv.display_stock, pv.free_shipping,
pl.name, pl.short
FROM products as p
INNER JOIN product_vendors AS pv ON pv.product_id = p.id AND pv.vendor_id = ?
LEFT JOIN product_languages AS pl ON pl.product_id = p.id AND pl.locale = ?
WHERE p.id in (SELECT product_id FROM newsletter_products where newsletter_id = ?)
ORDER BY "
@matatabi
matatabi / application_helper.rb
Created January 28, 2011 17:23
nested_attributes の使用例 (フォーム内で子オブジェクトを参照する方法含む)
def link_to_remove_fields(name, f)
# See corresponding remove_fields function in application.js
f.hidden_field(:_destroy) + link_to(name, nil, :onclick => "remove_fields(this)", :remote=>true)
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
@matatabi
matatabi / errors_i18n.rb
Created October 18, 2010 14:41
nested_attributes を使った際にエラーメッセージを正しく表示させる
# config/initializers/errors_i18n.rb
ActiveModel::Errors.module_eval do
def full_messages
full_messages = []
each do |attribute, messages|
messages = Array.wrap(messages)
next if messages.empty?