Ruby on rails 没有与[POST]匹配的路线”/用户/1/联系人/new“;
app/views/contacts/new.html.erbRuby on rails 没有与[POST]匹配的路线”/用户/1/联系人/new“;,ruby-on-rails,ruby,routes,Ruby On Rails,Ruby,Routes,app/views/contacts/new.html.erb <h2>Add New Contact</h2> <div class="row"> <div class="col-md-8"> <%= simple_form_for @contact, :url => new_user_contact_path do |f| %> <div class="form-grou
<h2>Add New Contact</h2>
<div class="row">
<div class="col-md-8">
<%= simple_form_for @contact, :url => new_user_contact_path do |f| %>
<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field :first_name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field :last_name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :date_of_birth, as: :date, discard_year:true, order:[:day, :month] %>
</div>
<div class="form-group">
<%= f.label :sex %>
<%= f.select :sex, [:male, :female], class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :address %>
<%= f.text_area :address, class: "form-control" %>
</div>
<div class="form-group">
<%= f.button :submit, "Submit", class: "btn btn-success btn-md btn-block" %>
</div>
</div>
<% end %>
</div>
</div>
<%= link_to "Back", user_contacts_path %>
config/routes.rb
Rails.application.routes.draw do
devise_for :users
resources :users do
resources :contacts
end
end
我的新建
和在联系人控制器中创建
操作
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
@contact.user = current_user
if @contact.save
flash[:success] = "Contact was successfully created!"
redirect_to @contact.user
else
render :new
end
end
这就是问题所在:
我在提交表单时收到错误:No route matches[POST]“/users/1/contacts/new”
。可能是什么问题?这:
<%= simple_form_for @contact, :url => new_user_contact_path do |f| %>
new_user_contact_path do|f|%>
应该是:
<%= simple_form_for @contact, :url => user_contacts_path do |f| %>
user_contacts_path do|f|%>
这是:
new_user_contact_path do|f|%>
应该是:
<%= simple_form_for @contact, :url => user_contacts_path do |f| %>
user_contacts_path do|f|%>
您只是发错了邮件。任何结尾带有/new的内容通常都是get
。创建路线不会以new
结束,您只是发布到了错误的路线。任何结尾带有/new的内容通常都是get
。创建路由不会以new
结束,当我这样做时,我会得到一个错误:未初始化的UsersController。这应该会发生吗?考虑到我正在使用Deviate。我认为这应该会影响联系人控制器:POST/users/:user_id/contacts(:format)contacts#create
是否确实需要将联系人资源嵌套在user中?我不知道你的应用程序的逻辑。在不嵌套路由的情况下,可以使联系人属于创建用户,我认为将资源分开比较干净。你能发布你的路由.rb
?应用程序的逻辑是,一个用户有许多联系人,联系人属于一个用户。它只是一个简单的web应用程序,用于测试下载到pdf的功能。routes.rb在上面。是的——我不会嵌套联系人路由,只是在创建联系人之前将用户的id设置为当前用户的id。然后在contact show页面上将contact实例变量定义为current_user.contacts。当我这样做时,我会得到一个错误:未初始化的UsersController。这应该会发生吗?考虑到我正在使用Deviate。我认为这应该会影响联系人控制器:POST/users/:user_id/contacts(:format)contacts#create
是否确实需要将联系人资源嵌套在user中?我不知道你的应用程序的逻辑。在不嵌套路由的情况下,可以使联系人属于创建用户,我认为将资源分开比较干净。你能发布你的路由.rb
?应用程序的逻辑是,一个用户有许多联系人,联系人属于一个用户。它只是一个简单的web应用程序,用于测试下载到pdf的功能。routes.rb在上面。是的——我不会嵌套联系人路由,只是在创建联系人之前将用户的id设置为当前用户的id。然后在contact show页面上将contact实例变量定义为current_user.contacts。