Last active
June 16, 2016 03:09
-
-
Save tienshunlo/0a181a98b3f2fa709f456000de066d6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> , | |
age<%= select_tag "ic_old[#{old_ic.id}][age]" , options_for_select(age_list , old_ic.age) %></select> | |
<%= select_tag "profile[profile_option_attributes][][option_id]", options_from_collection_for_select(issue.option, "id" ,"content"), {:class =>"form-control"} %> | |
<span class="add">add</span> | |
<span class="del">del</span> | |
(<%= old_ic.id %>) | |
</li> | |
<% end %> | |
</ol> | |
<button type="submit">Submit</button> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
member do | |
post :profile_option_save | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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'> | |
<div class="form-group"> | |
<%= label_tag "地點:" %> <%= f.text_field :location, class:"form-control" %> | |
</div> | |
<div class="form-group"> | |
<%= label_tag "性別:" %> <%= f.select :gender, Profile::GENDER_TYPES, class:"form-control" %> | |
</div> | |
<% @issues.each do |issue|%> | |
<div class="form-group"> | |
<%= issue.content %> | |
<%= select_tag "profile[profile_option_attributes][][option_id]", options_from_collection_for_select(issue.option, "id" ,"content"), {:class =>"form-control"} %> | |
</div> | |
<% end %> | |
</div> | |
<div class="form-group"> | |
<%= f.submit "送出", class: "btn btn-primary" %> | |
</div> | |
</div> | |
</div> | |
<% end %> | |
</li> | |
</ul> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dashboard::ProfilesController < Dashboard::DashboardController | |
before_action :find_user | |
before_action :set_profile | |
def new | |
@profile = @user.build_profile | |
#id:4是登入類別 | |
@issues = SpecialCate.find(4).issue | |
#@profile_option = ProfileOption.new(:profile_id => current_user) | |
@profile_option = @profile.profile_option.new(:profile_id => current_user) | |
end | |
def edit | |
end | |
def create | |
#User.create(:name => params[:user][:name]) | |
#@profile = @user.build_profile(:location=>params[:profile][:location], :gender=>params[:profile][:gender], :profile_option => params[:profile][:profile_option][:option_id]) | |
@profile = @user.build_profile(profile_params) | |
if @profile.save | |
#redirect_to dashboard_user_path(current_user) | |
redirect_to special_cates_dashboard_user_responds_path(current_user) | |
else | |
render new | |
end | |
end | |
def update | |
@profile = @user.profile | |
if @profile.update(profile_params) | |
redirect_to dashboard_user_path(current_user) | |
else | |
render edit | |
end | |
end | |
private | |
def profile_params | |
#測試用 | |
#params.require(:profile).permit(:location, :gender, :option_ids =>[]) | |
#測試用 | |
#可以用的 | |
params.require(:profile).permit(:location, :gender, profile_option_attributes:[:option_id]) | |
#可以用的 | |
#params.require(:profile).permit(:location, :gender, :option_ids => []) | |
#params.require(:profile).permit(:location, :gender, :option_ids => [], :basic_option_a, :basic_option_b, :basic_option_c, :basic_option_d, :basic_option_e) | |
end | |
def find_user | |
@user = current_user | |
end | |
def set_profile | |
@profile = @user.profile | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dashboard::ProfilesController < Dashboard::DashboardController | |
before_action :find_user | |
before_action :set_profile | |
def new | |
@profile = @user.build_profile | |
#id:4是登入類別 | |
@issues = SpecialCate.find(4).issue | |
#@profile_option = ProfileOption.new(:profile_id => current_user) | |
@profile_option = @profile.profile_option.new(:profile_id => current_user) | |
end | |
def edit | |
end | |
def create | |
@profile = @user.build_profile(params[:profile][:location], params[:profile][:location]) | |
params[profile_option_attributes][][option_id].each_index do |index| | |
ProfileOption.create(:profile_id => @profile.id , :option_id => params[profile_option_attributes][][option_id][index]) | |
end | |
if @profile.save | |
#redirect_to dashboard_user_path(current_user) | |
redirect_to special_cates_dashboard_user_responds_path(current_user) | |
else | |
render new | |
end | |
end | |
def update | |
@profile = @user.profile | |
if @profile.update(profile_params) | |
redirect_to dashboard_user_path(current_user) | |
else | |
render edit | |
end | |
----- | |
#取出所有關連id | |
ids = (params[:ic_old] || {}).keys.map{|i|i.to_i} | |
#語意:排除送出的id之外的所有隸屬item的都刪除 | |
ItemChild.where("item_id = #{@item.id} AND id NOT IN (#{ids.join(',')})").delete_all | |
#更新舊的資料 | |
if params[:ic_old] | |
params[:ic_old].each_pair do |id , data| | |
ic = ItemChild.where(:item_id => @item.id , :id => id).first | |
if ic | |
#這邊要過 permit 或是一個一個指定都行 | |
ic.update_attributes(:name => data[:name] , :age => data[:age]) | |
end | |
end | |
end | |
#額外新增的都再塞入 | |
if params[:ic] | |
params[:ic][:name].each_index do |index| | |
ItemChild.create(:item_id => @item.id , :name => params[:ic][:name][index] , :age => params[:ic][:age][index]) | |
end | |
end | |
redirect_to :back | |
end | |
end | |
private | |
def profile_params | |
#測試用 | |
#params.require(:profile).permit(:location, :gender, :option_ids =>[]) | |
#測試用 | |
#可以用的 | |
params.require(:profile).permit(:location, :gender, profile_option_attributes:[:option_id]) | |
#可以用的 | |
#params.require(:profile).permit(:location, :gender, :option_ids => []) | |
#params.require(:profile).permit(:location, :gender, :option_ids => [], :basic_option_a, :basic_option_b, :basic_option_c, :basic_option_d, :basic_option_e) | |
end | |
def find_user | |
@user = current_user | |
end | |
def set_profile | |
@profile = @user.profile | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment