我有一个医生姓名的下拉列表。我希望能够从下拉列表中选择一位医生,点击编辑并转到医生的编辑页面。
在我的路线中:
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的值在那里,它也被列入白名单。我不确定发生了什么。
答案 0 :(得分:1)
我建议在这种情况下使用form_for而不是form_tag
,因为您要更新资源。 form_tag
最适合用于通过POST创建资源,但即便如此form_for
也是一种更方便的解决方案。
您的代码变为:
<%= form_for @physician do |f| %>
<div class="form-group space">
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
</div>
<div class="form-group space">
<%= link_to '', root_path, class: 'btn btn-primary glyphicon glyphicon-triangle-left' %>
<%= f.submit 'Update Physician', class: 'btn btn-warning pull-add-btn' %>
</div>
<% end %>