请帮忙 表单不保存collection_select,也不会编辑 _form.html.erb
<div class="field">
<%= f.label :company_id %><br />
<%= collection_select(:learner, :learner_id, @clients, :id, :name, prompt: >true) %>
</div>
learners_controller.rb
def new
@learner = Learner.new
@clients = Client.all
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @learner }
end
end
# GET /learners/1/edit
def edit
@learner = Learner.find(params[:learner][:learner_id])
end
# POST /learners
# POST /learners.json
def create
@learner = Learner.new(learner_params)
respond_to do |format|
if @learner.save
format.html { redirect_to @learner, notice: 'Learner was successfully created.' }
format.json { render :show, status: :created, location: @learner }
else
format.html { render :new }
format.json { render json: @learner.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /learners/1
# PATCH/PUT /learners/1.json
def update
respond_to do |format|
if @learner.update(learner_params)
format.html { redirect_to @learner, notice: 'Learner was successfully updated.' }
format.json { render :show, status: :ok, location: @learner }
else
format.html { render :edit }
format.json { render json: @learner.errors, status: :unprocessable_entity }
end
end
end
答案 0 :(得分:1)
我认为你应该这样做:
def edit
@learner = Learner.find(params[:learner][:learner_id])
end
找到学习者的记录。
答案 1 :(得分:0)
根据您在评论中发布的参数,编辑路线只会将ID传递为:id
,因此编辑操作应为:
def edit
@learner = Learner.find(params[:id])
end
对于不保存collection_select
输入的表单,看起来您正在使用Rails 4的默认strong parameters gem。这意味着learner_id
必须在列表中可用learner_params
列入白名单的参数。在控制器的底部应该是def learner_params
params.require(:learner).permit(...other parameters..., :learner_id)
end
方法。
{{1}}