Skip to content

Instantly share code, notes, and snippets.

@tienshunlo
tienshunlo / answer.rb
Last active August 29, 2015 14:23
surveysays
class Answer < ActiveRecord::Base
belongs_to :question
end
@tienshunlo
tienshunlo / Questionnaire.rb
Last active August 29, 2015 14:25
Rails 4 nested attributes and has_many :through associaton in a form
class Questionnaire < ActiveRecord::Base
has_many :questionnaire_surveys
has_many :surveys, through: :questionnaire_surveys
accepts_nested_attributes_for :questionnaire_surveys
end
1. MODEL
item has many item_childrens
-> user has one answersheet
2. CONTROLLER
item_controller -> user_controller
multi method -> useranswer method
3. CONTROLLER
def useranswer method end
@tienshunlo
tienshunlo / new.html.erb
Created June 15, 2016 23:52
view/dashboard/profile.html.erb
<div class='content_wrapper option_wrapper'>
<ul class = 'option_first_banner clearfix'>
<li class = 'option_right_banner'>
<%= form_for @profile, :url => dashboard_user_profiles_path, :method => :POST do |f| %>
<div class="edit_panel panel panel-default">
<div class="edit_heading panel-heading">
<h3 class="panel-title">個人資料</h3>
</div>
<div class="panel-body">
<div class='option_content'>
<%= form_tag multi_save_item_path do %>
<% age_list = (18..30).to_a %>
<ol>
<li class="template">
name<input data-name="ic[name][]"> ,
age<select data-name="ic[age][]"><%= options_for_select(age_list) %></select>
<span class="add">add</span><span class="del">del</span></li>
<% @item_childs.each do |old_ic| %>
<li class="old_record">
name<%= text_field_tag "ic_old[#{old_ic.id}][name]" , old_ic.name %> ,
@tienshunlo
tienshunlo / profile_controller.rb
Created June 16, 2016 14:12
更新profile的題目
def update
@profile = @user.profile
ids = @profile.profile_option.map{|t| t.option_id}
if ids.length > 0
ProfileOption.where("profile_id =? AND option_id NOT IN (#{ids.join(',')})", @profile.id).delete_all
else
ProfileOption.where("profile_id = ?", @profile.id).delete_all
class Option < ActiveRecord::Base
belongs_to :issue
has_many :respond, :dependent => :destroy
has_many :user, through: :respond
#可以用的
has_many :profile_option
has_many :profile, through: :profile_option
#可以用的
end