对不起,我已经看了类似的问题这一点,但仍然无法弄清楚出于某种原因...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