我正在使用rails开发一种基于web的游戏。我的目标是建立一个表单,允许用户通过用户更新表单在user/show页面上创建堡垒。我一直在使用嵌套的属性和关联来尝试解决这个问题,但我一直遇到错误。
我在我的模型中建立了关联,例如:
class Fort < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :forts
accepts_nested_attributes_for :forts
... (omitted validations)
end
我已经设置了我的用户控制器,更新和编辑操作如下:
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
@user.forts.build(params[:user][:forts])
if @user.update_attributes(user_params)
render @user
else
render 'users/show'
end
end
用户/显示视图中的"我的表单"如下所示:
<%= form_for @user do |f| %>
<%= f.fields_for :fort do |builder| %>
<%= builder.label :name, "Name" %>
<%= builder.text_field :name %>
<%= builder.label :ownership, "Ownership" %>
<%= builder.text_field :ownership %>
<% end %>
<%= f.submit %>
<% end %>
我的路线(我认为它们在这里不重要,但以防万一),
BR::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :forts
resources :encampments
root 'home#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
最后,我的参数是这样写的:
def user_params
params.require(:user).permit(:name, :email, :username, :password, :password_confirmation, forts_attributes: [ :name, :xco, :yco, :territory, :ownership ])
end
我在上述之间摇摆不定
def user_params
params.require(:user).permit(:name, :email, :username, :password, :password_confirmation, forts_ids: [ :name, :xco, :yco, :territory, :ownership ])
end
和
def user_params
params.require(:user).permit(:name, :email, :username, :password, :password_confirmation, :forts => [ :name, :xco, :yco, :territory, :ownership ])
end
基于我在其他Stack Overflow线程上看到的不同协议。这些排列都不起作用。
他们都在日志中抛出了这个错误:
Unpermitted parameters: fort
整个过程如下:
Started PATCH "/users/1" for 127.0.0.1 at 2014-09-04 20:35:02 -0400
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"srybKv+WDZubO4ez+kiwfTYZvrP4tZYe9QoxQaMgmPk=", "user"=>{"fort"=>{"name"=>"test2", "ownership"=>"43"}}, "commit"=>"Update User", "id"=>"1"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1"]]
Unpermitted parameters: fort
(0.1ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('ascottpedersen@gmail.com') AND "users"."id" != 1) LIMIT 1
(0.1ms) rollback transaction
Rendered users/show.html.erb within layouts/application (49.2ms)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '0705e5d60cc1be62fa51d8e407683e79f730a3b2' LIMIT 1
Completed 200 OK in 173ms (Views: 66.1ms | ActiveRecord: 5.1ms)
我很确定我把我的I点和T点都划掉了,但现在我正在努力找出我的错误。你们能提供的任何帮助,我都将永远感谢你们。我仍然可以想象的唯一问题是,我将更新表单放在我的用户/显示视图中,但我几乎可以肯定这是一件好事
再次感谢
Alex p
您的参数名称与user_params
方法中的名称不匹配:一个方法有fort
,另一个有forts
。
此外,要使嵌套属性工作,传递给fields_for
的名称必须与关联名称匹配(即:forts
这将导致forts的参数位于forts_attributes
键下(因此这是应该进入user_params
的内容,但只有当您将调用更改为fields_for
时)
由于您的User
模型中有has_many :forts
关系,因此此行
<%= f.fields_for :fort do |builder| %>
应该是
<%= f.fields_for :forts do |builder| %>
我想这就是你的问题所在。