如何将以下form_for
- collection_select
翻译为simple_form?
<%= collection_select(:service, :service_menu_id, @service_menus, :id, :name, prompt: true) %>
要...
<%= f.input ...
要求更新:
<div class="col-xs-4">
<%= simple_form_for @service do |f| %>
<div class="field">
<!--Working - selected value saves to db-->
<%= collection_select(:service, :service_menu_id, ServiceMenu.all, :id, :name, {:prompt => true }) %>
<!--Selected value does not save to db-->
<%#= f.input :service_menu, :collection => @service_menus, label_method: :name, value_method: :id, :include_blank => false, prompt: "Select a main service" %>
<%= f.simple_fields_for :styles do |task| %>
<%= render 'style_fields', :f => task %>
<% end %>
</div>
<div class="links">
<%= link_to_add_association 'Add New Style', f, :styles, class: 'btn btn-success' %>
</div><br>
<div class="actions">
<%= f.submit :class => 'btn btn-primary'%>
</div>
<% end %>
控制器
class ServicesController < ApplicationController
before_action :set_service, only: [:show, :edit, :update, :destroy]
def index
@services = current_tech.services
end
def show
end
def new
@service = current_tech.services.build
@service_menus = ServiceMenu.all
end
def create
@service = current_tech.services.build(service_params)
respond_to do |format|
if @service.save
format.html { redirect_to @service, notice: 'Service was successfully created.' }
format.json { render :show, status: :created, location: @service }
else
format.html { render :new }
format.json { render json: @service.errors, status: :unprocessable_entity }
end
end
end
def edit
@service_menus = ServiceMenu.all
end
def update
respond_to do |format|
if @service.update(service_params)
format.html { redirect_to @service, notice: 'Service was successfully updated.' }
format.json { render :show, status: :ok, location: @service }
else
format.html { render :edit }
format.json { render json: @service.errors, status: :unprocessable_entity }
end
end
end
def destroy
@service.destroy
respond_to do |format|
format.html { redirect_to services_url, notice: 'Service was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_service
@service = Service.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def service_params
params.require(:service).permit(:name, :service_menu_id, styles_attributes: [:id, :tech_id, :name, :hours, :minutes, :price, :details, :_destroy])
end
end
答案 0 :(得分:1)
如果您可以将其包装为 表单对象 (即f.collection_select
),那么这将有效
<%= f.input :service_menu, :collection => @service_menus, :label_method => :name, :value_method => :id, :selected => @service.service_menu_id :include_blank => true %>