2014-09-19 60 views
0

我想獲得一個腳本來分配提交表單時的product_id的值。我的腳本現在總是將值賦值爲1.我需要它來分配產品的product_id。我注意到,當我提交它時,實際上會將選擇更改爲與id 1相同的選擇,所以我把它重寫爲1的腳本弄亂了?JavaScript上分配紅寶石的軌道形式的值

Here是一個JSFiddle,它似乎在html中正常工作,但在我的ruby代碼中沒有。

腳本:

$(function(){ 
$("#commit").on("click", function(){ 
    var valueAssigned = false; 
    $('select').each(function(){ 
     if($(this).val()) 
     { 
      $("[name='question[product_id]']").val($(this).val()); 
      valueAssigned = true; 
      return; 
     } 
    }); 
}); 
}); 

形式:請注意,只有collection_select的一個基於選擇的類別中。腳本根據選擇顯示並隱藏它們。

<%= form_for @question, url: new_tf_question_path(@question) do |f| %> 

<%= render 'shared/error_questions' %> 

<%= f.label :category %><br> 
<%= f.select :category, [ ["IP Voice Telephony", "ip_voice"], ["IP Video Surveillance", "ip_video_surveillance"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"] ], {prompt: "Select Category"}, class: "input-lg", :id => "category" %> 

<div id="ip_voice"> 
<%= f.label :product_id %><br> 
<%= f.collection_select :product_id, Product.where({ category: "ip_voice" }), :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg", id: "question_product_id1", name: "voice"} %> 
</div> 

<div id="ip_video_surveillance"> 
<%= f.label :product_id %><br> 
<%= f.collection_select :product_id, Product.where({ category: "ip_video_surveillance" }), :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg", id: "question_product_id2", name: "surv"} %> 
</div> 

<div id="ip_video_telephony"> 
<%= f.label :product_id %><br> 
<%= f.collection_select :product_id, Product.where({ category: "ip_video_telephony" }), :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg", id: "question_product_id2", name: "video"} %> 
</div> 

<div id="consumer_atas"> 
<%= f.label :product_id %><br> 
<%= f.collection_select :product_id, Product.where({ category: "consumer_atas" }), :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg", id: "question_product_id3", name: "atas"} %> 
</div> 

<div id="enterprise_gateways"> 
<%= f.label :product_id %><br> 
<%= f.collection_select :product_id, Product.where({ category: "enterprise_gateways" }), :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg", id: "question_product_id4", name: "gate"} %> 
</div> 

<div id="ip_pbx"> 
<%= f.label :product_id %><br> 
<%= f.collection_select :product_id, Product.where({ category: "ip_pbx" }), :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg", id: "question_product_id5", name: "pbx"} %> 
</div> 

<input id="targetField" type="hidden" name="question[product_id]" value="0"/> 

<%= f.label :section %><br> 
<%= f.collection_select :section, @sections, :id, :name, {prompt: "Select a section"}, {class: "form-control input-lg" } %> 

<%= f.label :active %><br> 
<%= f.check_box :active %> 


<%= f.fields_for :answers do |builder| %> 

<%= render 'tf_answers', :f => builder %> 

<% end %> 

<%= f.submit "Create Question", class: "btn btn-lg btn-primary", id: "commit", style: "margin-top: 45px;" %> 

<% end %> 

我不擅長的JavaScript,所以任何幫助表示讚賞。

編輯

QuestionController

def new 
@question = Question.new  
@products = Product.all 
end 
def new_mc 
@sections = Section.all 
@question = Question.new 
4.times { @question.answers.build } 
end 
def new_tf 
@sections = Section.all 
@question = Question.new 
@question.answers.build 
end 

# GET /questions/1/edit 
def edit 
@sections = Section.all 
end 

# POST /questions 
# POST /questions.json 
def create 

if params[:ip_video_telephony_product_id] != nil 
    params[:product_id] = params[:ip_video_telephony_product_id] 
end 
#:enterprise_gateways_product_id, :ip_video_surveillance_product_id, :ip_voice_product_id, :consumer_atas_product_id 
@question = Question.new(question_params + params[:product_id]) 
respond_to do |format| 
    if @question.save 
    @answer = Answer.find_by_question_id(@question.id) 
    if @answer.option == "True" 
     Answer.create(option: "False", question_id: @answer.question_id, correct: false) 
    end 
    if @answer.option == "False" 
     Answer.create(option: "True", question_id: @answer.question_id, correct: false) 
    end 
    format.html { redirect_to @question, notice: 'Question was successfully created.' } 
    format.json { render action: 'show', status: :created, location: @question } 
    else 
    format.html { render action: 'new' } 
    format.json { render json: @question.errors, status: :unprocessable_entity } 
    end 
end 
end 
.... 
def question_params 
    params.require(:question).permit(:ip_video_telephony_product_id, :enterprise_gateways_product_id, :ip_video_surveillance_product_id, :ip_voice_product_id, :consumer_atas_product_id, :section, :content, :question_type, :category, :product_id, :active, :user_id, answers_attributes: [ :option, :correct, :question_id ]). 
    merge user_id: current_user.id 
end 

登錄:

Started POST "/tf_question" for 127.0.0.1 at 2014-09-19 17:09:59 -0700 
Processing by QuestionsController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x", "question"=>{"question_type"=>"TF", "content"=>"damn", "category"=>"ip_video_telephony", "section"=>"1", "active"=>"1", "answers_attributes"=>{"0"=>{"correct"=>"1", "option"=>"True"}}}, "voice"=>"", "surv"=>"", "video"=>"20", "atas"=>"", "gate"=>"", "pbx"=>"", "commit"=>"Create Question"} 
User Load (2.6ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'x' LIMIT 1 
(0.1ms) begin transaction 
(0.0ms) rollback transaction 
Completed 500 Internal Server Error in 23ms 

新形式:我只是改變了所有論文並刪除腳本。

<div id="ip_voice"> 
<%= f.label :product_id %><br> 
<%= f.collection_select :ip_voice_product_id, Product.where({ category: "ip_voice" }), :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg", id: "question_product_id1", name: "voice"} %> 
</div> 

回答

1

雖然我不贊成你目前的形式排列的,但是既然你說只有一個選擇將是可見的,你可以使用:visible選擇的jQuery。你可以改變你的JS單擊處理程序是這樣的,假設你增加了一個question_product_id類所有這些選擇元素

var value = $('select.question_product_id:visible').val(); 
$("[name='question[product_id]']").val(value); 

但是,如果是我寫這樣的形式,我不會刻意設置選擇值使用JS代碼,但我會添加一堆實例屬性問題模型(例如ip_video_telephony_product_id),並將它們用於f.collection_select :ip_video_telephony_product_id調用,然後在控制器或模型中我根據類別值計算應使用的值那。

的模式將是這樣的

class Question < ActiveRecord::Base 
    attr_accessor :ip_video_telephony_product_id, :enterprise_gateways_product_id # etc ... 
end 

形式的選擇應該是這樣的

<div id="enterprise_gateways"> 
<%= f.label :ip_video_telephony_product_id %><br> 
<%= f.collection_select :ip_video_telephony_product_id, Product.where({ category: "enterprise_gateways" }), :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg"} %> 
</div> 

由於屬性名稱是一致的,我們能做到類似於控制器中的東西(我更喜歡模型,但我們現在保持簡單)

def create 
    @question = Question.new(question_params) 
    @question.product_id = question_params["#{question_params[:category]}_product_id"] 
    # ... 
end 
+1

偉大的工程,我不得不刪除在你提到的形式的HTML設置名稱。 – DDDD 2014-09-20 00:48:15