У меня есть много-много отношений со студентами и организациями. При создании нового ученика я хочу установить флажок, чтобы выбрать одну или несколько организаций и сохранить это. Как мне это сделать? Как выглядит MVC конкретно? Я не могу найти ни одного онлайн-ресурса, который дает мне весь обзор.имеет много сквозных флажков?
Обновленный код:
в моей форме частичной:
<%= simple_form_for (@student) do |f| %>
<div class="field">
<%= f.label :organization_ids %><br />
<%= collection_check_boxes :student, :organization_ids, Organization.all, :id, :name %>
</div>
<div class="actions">
<button class="btn btn-primary" type="submit">Save</button> <%= link_to "Cancel", :back, {:class=>"btn btn-primary"} %>
</div>
<% end %>
контроллера:
def update
@student = Student.find(params[:id])
if Student.save_existing(params[:id], params[:student])
flash[:notice] = "Student was successfully updated!"
redirect_to students_path
else
flash[:error] = "Student failed to update."
redirect_to students_path
end
end
def student_params
params.require(:student).permit(:name, :organization_ids => [])
end
студент таблицы:
create_table "students", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "organization_id"
t.integer "organization_ids"
end
моя проблема: когда я делаю @student.organization_ids.inspect
, это дает мне пустой массив означает, что форма не сохраняет мои формы ввода флажков
Можете ли вы обновить свое сообщение с помощью кода модели. – Pavan
Этот Q/A http://stackoverflow.com/questions/7698952/using-check-boxes-with-a-has-many-relationship может помочь вам – MrYoshiji
Это весь код формы? – Pavan