私はチェックされた値を返す方法がわかりません。Rails、checkbox
@cities = Database.find(:all)
params[:cities].each do |city|
...
end
と
<% for city in @cities %>
<%= check_box_tag "cities[]", city.id %> <%= city.city1 %>
<% end %>
をが、エラーが発生しました:私のコードは
controller:
def index
// there I would like to get checked cities
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @cities }
end
end
viewer:
<%= render 'form' %>
_form:
<% for cities in Database.find(:all) %>
<div>
<%= check_box_tag cities.city1 %>
<%= cities.city1 %>
</div>
<% end %>
ありEDIT
は、私が試した
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
もう1つのことは、ビューからdbにリクエストを行うことです。コントローラー内で行う必要があります。 – Bohdan
コントローラーからdbへの要求を試みましたが、エラーが発生しました。私はparams [:cities]と何か間違っていると思う。 .. これがなければボックスをチェックすることができます。 – Martynas
POSTとGETリクエストの違いはありません。パラメータ:[cties]を使用可能にするには、フォームからデータを送信する必要があります。 – Bohdan