###現状のコード
ruby
1#コントローラー 2params[:coordinator][:section_id] = params[:coordinator][:section_id].blank? ? -1 : params[:coordinator][:section_id]
###やりたいこと
params[:coordinator][:section_id]
の値は""
です。
""
の場合は.blank?
で判定して-1
を代入するようにしています。
http://qiita.com/quattro_4/items/88578a2044c423cab3f0
の方法でシンプルに書けるかと思ったのですが、nilまたは空白ではないため、うまくいきません。
シンプルに書くにはどのように記述すれば良いでしょうか?
何卒、よろしくお願い致します。
###追記
coordinators_controller.rb
ruby
1class CoordinatorsController < ApplicationController 2 before_action :set_coordinator, only: [:show, :edit, :update, :destroy] 3 4 # GET /coordinators 5 # GET /coordinators.json 6 def index 7 @coordinators = Coordinator.all 8 end 9 10 # GET /coordinators/1 11 # GET /coordinators/1.json 12 def show 13 end 14 15 # GET /coordinators/regist 16 def regist 17 @coordinator = Coordinator.new 18 end 19 20 # GET /coordinators/1/edit 21 def edit 22 end 23 24 # POST /coordinators 25 # POST /coordinators.json 26 def create 27 @coordinator = Coordinator.new(coordinator_params) 28 respond_to do |format| 29 if @coordinator.save 30 format.html { redirect_to @coordinator, notice: 'コーディネーター登録完了' } 31 #format.json { render :show, status: :created, location: @coordinator } 32 else 33 format.html { render :regist } 34 #format.json { render json: @coordinator.errors, status: :unprocessable_entity } 35 end 36 end 37 end 38 39 # PATCH/PUT /coordinators/1 40 # PATCH/PUT /coordinators/1.json 41 def update 42 #params[:coordinator][:section_id] = params[:coordinator][:section_id].blank? ? -1 : params[:coordinator][:section_id] 43 respond_to do |format| 44 if @coordinator.update(coordinator_params) 45 format.html { redirect_to @coordinator, notice: 'Coordinator was successfully updated.' } 46 format.json { render :show, status: :ok, location: @coordinator } 47 else 48 format.html { render :edit } 49 format.json { render json: @coordinator.errors, status: :unprocessable_entity } 50 end 51 end 52 end 53 54 # DELETE /coordinators/1 55 # DELETE /coordinators/1.json 56 def destroy 57 @coordinator.destroy 58 respond_to do |format| 59 format.html { redirect_to coordinators_url, notice: 'Coordinator was successfully destroyed.' } 60 format.json { head :no_content } 61 end 62 end 63 64 private 65 # Use callbacks to share common setup or constraints between actions. 66 def set_coordinator 67 @coordinator = Coordinator.find(params[:id]) 68 end 69 70 # Never trust parameters from the scary internet, only allow the white list through. 71 def coordinator_params 72 params.require(:coordinator).permit(:name, :name2, :coordinator_id, :password, :mail, :section_id, :department_id, :division_id, :position_id, :profimg, :authority_id, :status, :token, :token_limit, {:office_ids => []}) 73 end 74end 75
coordinator.rb
ruby
1class Coordinator < ApplicationRecord 2 after_initialize :set_default 3 private 4 def set_default 5 self.section_id ||= -1 6 end 7end
###さらに追記
regist.html.erb
ruby
1<h1>New Coordinator</h1> 2 3<%= render 'form', coordinator: @coordinator %> 4 5<%= link_to 'Back', coordinators_path %>
_form.html.erb(一部:section_idのみ)
ruby
1<div class="field"> 2 <%= f.label :section, '事業課' %> 3 <%= f.collection_select( :section_id, Section.all, :id, :name, :include_blank => true) %> 4 </div>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/08 09:24 編集
2017/05/08 09:33 編集
2017/05/08 09:33
2017/05/08 09:33
2017/05/08 09:36 編集
2017/05/08 09:45 編集
2017/05/08 09:44 編集
2017/05/08 09:46
2017/05/08 09:56 編集
2017/05/08 10:07 編集
2017/05/08 10:08 編集
2017/05/08 10:11 編集
2017/05/08 10:18 編集
2017/05/08 10:28
2017/05/08 10:30
2017/05/08 10:48 編集
2017/05/08 10:49