對不起,我已經看了類似的問題這一點,但仍然無法弄清楚出於某種原因...Rails的5 Simpleform沒有更新嵌套屬性
我使用的設計,我試圖更新我的用戶模型和City模型(通過編輯註冊頁面)使用嵌套表單。
我正在基於數據庫中的一組預定義值更新用戶城市。所以用戶所要做的就是從列表中選擇一個城市,然後保存。
現在發生的所有事情都是表單提交,但城市沒有改變。
在此先感謝!
#user.rb
class User < ApplicationRecord
belongs_to :city
end
#city.rb
class City < ApplicationRecord
has_many :users
end
我的控制器:
class UsersController < ApplicationController
def index
@users = User.all
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to @user
else
render 'edit'
end
end
def show
@user = User.find(params[:id])
end
private
def user_params
params.require(:user).permit(city_attributes: [:id, :name, :user_id])
end
end
我的形式:
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= f.simple_fields_for :city do |c| %>
<%= c.input :city_id, collection: City.all.order(name: :asc), label_method: :name, value_method: :id, label: "City", include_blank: false, selected: @user.city_id %>
<% end %>
<% end %>
我的猜測是,它不起作用,因爲表單不會回發給你的users_controller。您必須以特定方式覆蓋'routes.rb'文件,以告訴Devise使用您的控制器。 – Iceman
謝謝@Iceman。我現在在下面發佈我的答案,我的問題已排序。嵌套屬性仍然不起作用,但這是我回到另一個時間。 –
很高興你的工作,也許我會考慮在其他問題的某個時候。 – Iceman