在Rails应用程序中找不到“新”方法,但它存在

时间:2017-11-04 14:14:18

标签: ruby-on-rails model-view-controller

如果这是一个愚蠢的问题,请提前道歉。我正在从零开始学习Rails,所以感谢与我合作。

所以,我有一个'人'模型,如下所示

class Person < ActiveRecord::Base
    belongs_to :team
    has_and_belongs_to_many :events, through: :event_people
    validates :first_name, presence: true, length: { maximum: 255 }
    validates :last_name, presence: true, length: { maximum: 255 }
    validates :email, presence: true, length: { maximum: 255 }
end

并创建了一个表单来创建一个具有关联团队ID的新人

<h1>Add new person</h1>
  <div class="row">
    <div class="col-md-8">
     <%= form_with url: new_person_path  do |person| %>
       <div class="form-group">
        <%= person.label :first_name %>
        <%= person.text_field :name, id: :person_first_name, class:"form-control" %>
   </div> 

 <div class="form-group">
    <%= person.label :last_name %>
    <%= person.text_field :name, id: :person_last_name, class:"form-control" %>
</div>
<div class="form-group">
    <%= person.label :email %>
    <%= person.text_field :email, id: :person_email, class:"form-control" %>
</div>
<div class="form-group">
    <%= person.label :job_title %>
    <%= person.text_field :job_title, id: :person_job_title, class:"form-control" %>
</div>

<div class="form-group">
    <% @teams = Team.all %>
    <%= person.label :team %>
    <%= person.select(:team_id, @teams.collect{|a| [a.name, a.id]},  { include_blank: true }, {:class => "form-control"}) %>
</div>
<div class="actions">
<%= person.submit 'Submit button', {:class => 'btn btn-primary'}%>
</div>
<% end %>
</div>
</div>

和我的routes.rb文件设置如下:

Rails.application.routes.draw do
    resources :people
    resources :teams
    resources :organisations
    resources :events
end

但每当我提交表单来创建新人时,我在控制台中都会收到以下错误:

POST http://localhost:3000/people/new 404 (Not Found)

事件尽管我的people_controller.rb中存在相关方法:

class PeopleController < ApplicationController
def index
    @people = Person.all
end
def show
    @people = Person.find(params[:id])
end
def new
    @people = Person.new
     respond_to do |format|
        format.html  # new.html.erb
        format.json  { render :json => @people }
      end
end

def create
   @people = Person.new(params[:person])

  respond_to do |format|
    if @people.save
      format.html  { redirect_to(@people,
                    :notice => 'Person was successfully created.') }
      format.json  { render :json => @people,
                    :status => :created, :location => @people }
    else
      format.html  { render :action => "new" }
      format.json  { render :json => @people.errors,
                    :status => :unprocessable_entity }
    end
  end
end

任何人都可以提出我可能做错的建议吗?

2 个答案:

答案 0 :(得分:1)

如果您有基础模型,则需要使用<%= form_with model: @people do |person| %>

参考:http://api.rubyonrails.org/v5.1/classes/ActionView/Helpers/FormHelper.html#method-i-form_with

答案 1 :(得分:0)

您发布的方法至少据我所知,应该输出一个空表单来输入您的人的属性。

首先尝试GET,这与REST URL的rails解释一致。可能,正确的路线最后也没有/new

接下来,尝试rails routes列出所有可用路线。

顺便说一句,如果在开发中,rails应该为您提供一个有意义的大型帮助页面,可能包括可用的路径。检查development.rb是否有一些标志启用它。