2013-04-12 2 views
0

그래서 레일스에 매우 기본적인 폼이 있습니다. 하지만 몇 가지 기능을 추가하기로했습니다. 약간의 문맥.
남자 또는 여자 중 하나를 선택하면 양식에서 일부 필드가 생성됩니다. 이미 V1에있는 필드가 있었지만 javascript로 생성하면 DB에 저장되지 않습니다. 나는 이유를 정말로 이해하지 못한다. 여기에 몇 가지 코드가 있습니다.DB에 js가 생성 한 필드를 레일에 저장

컨트롤러

def create 
    @pokemon = Pokemon.new(params[:pokefuck]) 

    respond_to do |format| 
    if @pokemon.save 
     format.html { redirect_to '/pokemons', notice: 'pokemon was successfully created.' } 
     format.json { render json: @pokemon, status: :created, location: @pokemon } 
    else 
     format.html { render action: "new" } 
     format.json { render json: @pokemon.errors, status: :unprocessable_entity } 
    end 
    end 
end 

보기

<%= f.select "size", options_for_select(size, @pokemon.size) %> 

크기가 작동하지 않습니다 하나를 만든 해시

입니다 작동

원래 필드

var s = document.createElement("select"); 
s.name = "po"; 
s.id = "pokemon_size"; 
sex.appendChild(s); 
for(i in size) { 
    document.formName.po.options[i] = new Option(size[i], size[i]); 
} 

새 요소의 이름이 [크기]가 아니기 때문에 그랬다고 생각했지만, 이것을 사용하면 document.formName.pokemon [size] .options [i] 라인이 더 이상 작동하지 않습니다.

그래서 나는이 작품을 어떻게 만들 수 있는지 알고 싶어합니다. 감사합니다.

+0

새 입력란이 즉시 만들어져 원본 DOM의 일부가 아니기 때문일 수 있습니다. – weltschmerz

+0

그래서 두 개의 다른 양식을 만들어야한다는 뜻입니까? Meh. – Simon

답변

1

양식 필드가 "pokefuck"으로 이름 공간이 지정되어 있다고 가정하면 양식 이름 요소를 "pokefuck [size]"로 선택하고 양식의 요소 컬렉션에서 해당 이름을 참조해야합니다.

var s = document.createElement("select"); 
s.name = "pokefuck[size]"; 
s.id = "pokemon_size"; 
sex.appendChild(s); 
for(i in size) { 
    document.formName.elements['pokefuck[size]'].options[i] = new Option(size[i], size[i]); 
} 
1

필드 이름을 params 해시에 입력해야합니다 (pokefuck).

pokefuck[size]은 양식 요소의 이름이어야합니다. 제출할 양식 요소에 양식 요소를 추가하십시오.