导轨形式 101

发布于 2025-01-07 12:07:00 字数 1591 浏览 0 评论 0原文

这是我的控制器:

class TalentController < ApplicationController
  def index
  end

  def new
    @talent = Talent.new
  end

  def create
        @talent.update_attributes!(params)
  end
end

这是我的 app/views/talent/new.html.haml:

= "Create new talent"

= form_for @talent, :url => {:action=>:create, :controller=>"talent"}, :method => :post do |f|
  = f.label :First_Name
  = f.text_field :first_name
  = f.label :Last_Name
  = f.text_field :last_name
  = f.label :City
  = f.text_field :city
  = f.label :State
  = f.text_field :state
  = f.label :Zip_code
  = f.text_field :zip_code
  = f.submit "Create"

当我点击创建按钮时,我收到此错误。

No route matches [POST] "/talent/new"

这是我的耙子路线:

                    /                          {:action=>"index", :controller=>"talent"}
talent_index GET    /talent(.:format)          {:action=>"index", :controller=>"talent"}
             POST   /talent(.:format)          {:action=>"create", :controller=>"talent"}
  new_talent GET    /talent/new(.:format)      {:action=>"new", :controller=>"talent"}
 edit_talent GET    /talent/:id/edit(.:format) {:action=>"edit", :controller=>"talent"}
      talent GET    /talent/:id(.:format)      {:action=>"show", :controller=>"talent"}
             PUT    /talent/:id(.:format)      {:action=>"update", :controller=>"talent"}
             DELETE /talent/:id(.:format)      {:action=>"destroy", :controller=>"talent"}

我在这里错过了什么?

here's my controller:

class TalentController < ApplicationController
  def index
  end

  def new
    @talent = Talent.new
  end

  def create
        @talent.update_attributes!(params)
  end
end

here's my app/views/talent/new.html.haml:

= "Create new talent"

= form_for @talent, :url => {:action=>:create, :controller=>"talent"}, :method => :post do |f|
  = f.label :First_Name
  = f.text_field :first_name
  = f.label :Last_Name
  = f.text_field :last_name
  = f.label :City
  = f.text_field :city
  = f.label :State
  = f.text_field :state
  = f.label :Zip_code
  = f.text_field :zip_code
  = f.submit "Create"

When I hit the create button, I get this error.

No route matches [POST] "/talent/new"

here's my rake routes:

                    /                          {:action=>"index", :controller=>"talent"}
talent_index GET    /talent(.:format)          {:action=>"index", :controller=>"talent"}
             POST   /talent(.:format)          {:action=>"create", :controller=>"talent"}
  new_talent GET    /talent/new(.:format)      {:action=>"new", :controller=>"talent"}
 edit_talent GET    /talent/:id/edit(.:format) {:action=>"edit", :controller=>"talent"}
      talent GET    /talent/:id(.:format)      {:action=>"show", :controller=>"talent"}
             PUT    /talent/:id(.:format)      {:action=>"update", :controller=>"talent"}
             DELETE /talent/:id(.:format)      {:action=>"destroy", :controller=>"talent"}

What did I miss here??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单气质女生网名 2025-01-14 12:07:00

我不知道为什么表单试图发布到new。但是,您的代码还需要进行一些改进。首先,没有必要将 :url:method 传递给 form_for。您唯一需要的是:

= form_for @talent do |f|

Rails 会自动处理其余的事情。

控制器的 create 方法中的代码不正确。当调用create时,它是一个新请求,因此@talent将是未定义的。你必须先设置它。第二个错误是您使用了update_attributes,它用于更新数据库中的现有记录。您想要创建一条新记录,因此必须使用create。另一种方法是简单地创建一个新的 Talent 实例并对其调用 save

所以,它应该看起来像这样:

def create
  @talent = Talent.create(params[:talent])
end

或者像这样:

def create
  @talent = Talent.new(params[:talent])
  if @talent.save
    # Saved successfully, do a redirect or something...
  else
    # Validation failed. Show form again so the user can fix his input.
    render :action => 'new'
  end
end

I don't know why the form is trying to post to new. However, there are some improvements to be made to your code. First of all it is not necessary to pass :url and :method to form_for. The only thing you need is:

= form_for @talent do |f|

Rails takes care of the rest automatically.

The code in the create method of your controller is incorrect. When create is called it is a new request and therefore @talent will be undefined. You have to set it first. The second mistake is that you are using update_attributes, which is used to update an existing record in the database. You want to create a new record, so you have to use create. Another way would be to simply create a new instance of Talent and call save on it.

So, it should look like this:

def create
  @talent = Talent.create(params[:talent])
end

Or like this:

def create
  @talent = Talent.new(params[:talent])
  if @talent.save
    # Saved successfully, do a redirect or something...
  else
    # Validation failed. Show form again so the user can fix his input.
    render :action => 'new'
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文