我有一個下拉菜單,裏面有醫生的名字。我希望能夠從下拉列表中選擇一位醫生,點擊編輯並轉到醫生的編輯頁面。從下拉菜單中選擇,編輯和更新Rails
在我的路線:
resources :physicians, only: [:index, :new, :create, :update]
get '/physicians/edit', to: 'physicians#edit'
在我的私人醫生控制器:
def update
@physician = Physician.find(params[:id])
respond_to do |format|
if @physican.update_attributes(physician_params)
format.html { redirect_to @physican, info: 'Physician was successfully updated.' }
else
format.html { render :edit }
end
end
end
def physician_params
params.require(:physician).permit(:office_id, :user_id, :full_name, :prefix, :first_name, :middle_name, :last_name, :suffix, :primary)
end
在我的私人醫生指數:
<%= form_tag '/physicians/edit', :method => :get do %>
<%= select_tag :id, options_from_collection_for_select(@physicians, :id, :first_name),
prompt: 'Choose one', id: 'physician_select', class: 'form-control option-large' %>
<%= submit_tag 'Edit', class: 'btn' %>
<% end %>
在我_form.edit.html.erb
<div class="center" role="form">
<%= form_tag physician_path(@physician), :method => :patch do %>
<div class="form-group space">
<%= label_tag :first_name %>
<%= text_field_tag :first_name, params[:first_name], class: 'form-control' %>
</div>
<div class="form-group space">
<%= link_to '', root_path, class: 'btn btn-primary glyphicon glyphicon-triangle-left' %>
<%= submit_tag 'Update Physician', class: 'btn btn-warning pull-add-btn' %>
</div>
<% end %>
</div>
FIRST_NAME屬性:試圖更新時
錯誤
ActionController::ParameterMissing in PhysiciansController#update
param is missing or the value is empty: physician
爲first_name的價值是存在的,它被列入白名單了。我不確定發生了什麼事。
你有:在physician_params允許FIRST_NAME?你能分享physician_params方法嗎? – Anand
我已經更新了醫師控制器以顯示允許的參數。 – bradbajuz
我看到你已經轉移到了form_for,這很好。在上面的form_tag代碼中,移除require(:physician)可能會訣竅 - 在form_tag的情況下,params中沒有醫師密鑰。 – Anand