Skip to content

Instantly share code, notes, and snippets.

@tienshunlo
Last active June 18, 2016 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tienshunlo/ebfaaa969311d9df3b15f388fb1e339a to your computer and use it in GitHub Desktop.
Save tienshunlo/ebfaaa969311d9df3b15f388fb1e339a to your computer and use it in GitHub Desktop.
<%= form_for current_user.profile, url: dashboard_user_profile_path(current_user, :id => :user_id), html: { method: :put, class: 'form-horizontal' } do |f| %>
<div class="dashboard_panel panel ">
<div class="panel-body">
<div class="form-group">
<%= f.label "地點" %><%= f.text_field :location, autofocus: true, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label "性別", style:"display:block" %><%= f.select :gender, Profile::GENDER_TYPES, class:"form-control" %>
</div>
<% @issues.each do |issue|%>
<div class="form-group">
<%= issue.content %>
#這邊主要是增加select_tag的第四個變數-可以顯示第一次輸進去的舊資料,不過edit之後就會變得很奇怪,所以我不確定這樣寫對不對:@profile.option.where(:issue_id => issue.id).first.id
<%= select_tag "profile[profile_option_attributes][][option_id]", options_from_collection_for_select(issue.option, "id" ,"content", @profile.option.where(:issue_id => issue.id).first.id), {:class =>"form-control"} %>
</div>
<% end %>
<div class="form-group">
<%= f.submit "Update", class: "btn btn-primary" %>
</div>
</div>
</div>
<% end %>
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
class Profile < ActiveRecord::Base
belongs_to :user
#可以用的
has_many :profile_option
has_many :option, through: :profile_option, dependent: :destroy
accepts_nested_attributes_for :profile_option
#可以用的
self.primary_key = 'user_id'
GENDER_TYPES = [ "Male", "Female", "Do not wish to say" ]
end
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 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
#@profile_option = @profile.profile_option.create(:id => current_user ,:profile_id => current_user)
#redirect_to dashboard_user_path(current_user)
redirect_to special_cates_dashboard_user_responds_path(current_user)
else
render new
end
end
def edit
end
def update
#以下是測試用,可以把新資料送出去和存到資料庫去,但是沒辦法把舊的刪掉
# ids = @profile.profile_option.map{|t| t.option_id} 這行應該改成下一行:
ids = params[:profile][:profile_option_attributes].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
end
@profile.profile_option.each do |f|
ic = ProfileOption.where(:profile_id => @profile.id, :id => f.id).first
if ic
ic.update_attributes(:option_id => f.option_id)
end
end
params[:profile][:profile_option_attributes].each do |index|
ProfileOption.create(:profile_id => @profile.id, :option_id => index[:option_id])
end
redirect_to dashboard_user_path(current_user)
#以上是測試用,可以把新資料送出去和存到資料庫去,但是沒辦法把舊的刪掉
#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)
#測試用
#可以用的
params.require(:profile).permit(:location, :gender, profile_option_attributes:[:id, :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
class ProfileOption < ActiveRecord::Base
belongs_to :profile
belongs_to :option
end
#好像沒有把舊的刪掉
Started PUT "/dashboard/users/2/profiles/user_id" for 118.165.16.66 at 2016-06-17 15:11:41 +0000
Cannot render console from 118.165.16.66! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Dashboard::ProfilesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"qOEwx0O+W3rk7eGn3g0SLZ7p01fZrK3NqC2ACYAO+H2Bgd6CR0AcPWye/aoq5myHaFULD2YuxauhsS0G4oGyiw==", "profile"=>{"location"=>"台北", "gender"=>"Male", "profile_option_attributes"=>[{"option_id"=>"125"}, {"option_id"=>"129"}, {"option_id"=>"133"}]}, "commit"=>"Update", "user_id"=>"2", "id"=>"user_id"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
Profile Load (0.5ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = ? LIMIT 1 [["user_id", 2]]
ProfileOption Load (0.6ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? [["profile_id", 2]]
SQL (0.6ms) DELETE FROM "profile_options" WHERE (profile_id =2 AND option_id NOT IN (112,116,120,114,118,122,114,118,122,113,117,121,114,118,122,114,118,122,126,130,134,123,127))
ProfileOption Load (0.5ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 99]]
(0.2ms) begin transaction
(0.2ms) commit transaction
ProfileOption Load (0.3ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 100]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 101]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.3ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 102]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 103]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.5ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 104]]
(0.2ms) begin transaction
(0.3ms) commit transaction
ProfileOption Load (0.7ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 105]]
(0.2ms) begin transaction
(0.2ms) commit transaction
ProfileOption Load (0.3ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 106]]
(0.2ms) begin transaction
(0.2ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 107]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.3ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 108]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 109]]
(0.3ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 110]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 111]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.1ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 112]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.1ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 113]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.1ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 114]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.1ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 115]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 116]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 123]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 124]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 125]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 127]]
(0.1ms) begin transaction
(0.1ms) commit transaction
ProfileOption Load (0.2ms) SELECT "profile_options".* FROM "profile_options" WHERE "profile_options"."profile_id" = ? AND "profile_options"."id" = ? ORDER BY "profile_options"."id" ASC LIMIT 1 [["profile_id", 2], ["id", 128]]
(0.1ms) begin transaction
(0.1ms) commit transaction
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "profile_options" ("profile_id", "option_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["profile_id", 2], ["option_id", 125], ["created_at", "2016-06-17 15:11:42.086523"], ["updated_at", "2016-06-17 15:11:42.086523"]]
(11.4ms) commit transaction
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "profile_options" ("profile_id", "option_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["profile_id", 2], ["option_id", 129], ["created_at", "2016-06-17 15:11:42.101491"], ["updated_at", "2016-06-17 15:11:42.101491"]]
(17.1ms) commit transaction
(0.2ms) begin transaction
SQL (0.5ms) INSERT INTO "profile_options" ("profile_id", "option_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["profile_id", 2], ["option_id", 133], ["created_at", "2016-06-17 15:11:42.122365"], ["updated_at", "2016-06-17 15:11:42.122365"]]
(34.2ms) commit transaction
Redirected to https://surveysays-cates-issue-option-tienshunlo.c9users.io/dashboard/users/2
Completed 302 Found in 259ms (ActiveRecord: 80.4ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment