Reputation:
Please help The form does not save the collection_select and it does not edit _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
Upvotes: 0
Views: 58
Reputation: 2052
From the parameters you posted in the comment, the edit route is passing in the ID as just :id
, so the edit action should be:
def edit
@learner = Learner.find(params[:id])
end
As for the form not saving your collection_select
input, it looks like you are using the default strong parameters gem with Rails 4. This means that learner_id
must be available in the list of whitelisted parameters. At the bottom of your controller should be the learner_params
method.
def learner_params
params.require(:learner).permit(...other parameters..., :learner_id)
end
Upvotes: 0
Reputation: 2653
I think you should do:
def edit
@learner = Learner.find(params[:learner][:learner_id])
end
To find the record of learner.
Upvotes: 1