Skip to content

Instantly share code, notes, and snippets.

@tienshunlo
Created June 15, 2016 23:52
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/de29d7d6847c67a3e2569f701abd036e to your computer and use it in GitHub Desktop.
Save tienshunlo/de29d7d6847c67a3e2569f701abd036e to your computer and use it in GitHub Desktop.
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'>
<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"} %>
<%#= select_tag "profile[option_ids]", options_from_collection_for_select(issue.option, "id" ,"content"), {:class =>"form-control"} %>
<%#= select_tag "profile[profile_option_attributes][][option_id]", issue.option.all.collect {|p| [ p.id, p.content] }, {:class =>"form-control"} %>
</div>
<% end %>
</div>
<div class="form-group">
<%= f.submit "送出", class: "btn btn-primary" %>
</div>
</div>
</div>
<% end %>
</li>
</ul>
</div>
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 ProfileOption < ActiveRecord::Base
belongs_to :profile
belongs_to :option
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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment