我知道我正在以某种方式破坏Rails惯例导致错误,但我无法弄清楚是什么......
# controller
def membership
@user = User.new(user_type:"member")
end
def membership_create
@user = User.find(params[:id])
...
end
# routes
match '/membership/create.:id', to: "users#membership_create", via: :post, as: "create_member"
# view
# have tried the following based on various other answers, doesn't work
<%= form_for @user, :url => create_member_path(@user), method: :post do |f| %>
<%= form_for @user, :url => create_member_path(@user) do |f| %>
始终收到错误消息:No route matches {:action=>"membership_create", :controller=>"users", :id=>nil} missing required keys: [:id]
答案 0 :(得分:2)
为什么不/membership/create/:id
而不是/membership/create.:id
?
但是,您只是从您的会员方法构建新用户:
def membership
@user = User.new(user_type:"member")
end
此用户还没有id
。因此,当您将其传递到表单的路径时:
<%= form_for @user, :url => create_member_path(@user) do |f| %>
id
为nil
。
但是,您的membership_create
需要id
? - (在路线中配置)请记住,此时此值为零。除非你奇迹般地在循环中的某个地方包含id
,这是我看不到的,或者这两种方法并没有像我想的那样直接相关。