Ruby on rails 当使用符号或实例变量时,rails是否形成_以使用相同的路由?

Ruby on rails 当使用符号或实例变量时,rails是否形成_以使用相同的路由?,ruby-on-rails,Ruby On Rails,这只是一个关于助手“form_for”的一般性问题。我正在用教科书编写一个程序,它有一个使用的表单文件。该表单由新建模板和编辑模板共享。但是,我已经看到很多教程使用符号(:product)而不是实例变量。所以,我试着交换它们,看看会发生什么。在尝试提交表单时,它碰巧给了我一个路由错误: No route matches [POST] "/products/new" 及 代码如下: <%= form_for(:product) do |f| %> <% if @pr

这只是一个关于助手“form_for”的一般性问题。我正在用教科书编写一个程序,它有一个使用
的表单文件。该表单由新建模板和编辑模板共享。但是,我已经看到很多教程使用符号(:product)而不是实例变量。所以,我试着交换它们,看看会发生什么。在尝试提交表单时,它碰巧给了我一个路由错误:

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

代码如下:

    <%= form_for(:product) do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% @product.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
我以为我读过“form_for:product”会搜索同名的实例变量,并使用相同的路径product_path,但似乎我错了。我看过这个网站上的其他帖子,但它们似乎没有提到路线。为什么我会犯这个错误


编辑:我将“url:products\u path”选项添加到form\u for:productline中,现在可以使用了。我猜符号不知道如何使用资源中的路由,比如@product do?

如果您有路由错误,请检查confg/routes.rb文件,它必须包括resources:products。如果没有,请添加它并重新启动服务器

是的,在路由中,我们使用带有表名的符号(模型的复数形式)来定义资源

编辑:


使用form_作为实例变量:@product,该变量在控制器上用空的新产品初始化,或查找到要更新的产品

嗯,这似乎不能解释路由问题。只有标签更改如果您有路由错误,请检查文件
confg/routes.rb
它必须包括
resources:products
。如果没有,添加它并重新启动服务器。它就在那里。使用符号是否算作资源?我知道使用@does,也知道路径。如果符号被实例化为一个对象,就像使用
@
一样,那么是的,它将具有与路由相同的行为。除非您定义希望form_for方法如何发布,否则form_for中的对象将调用POST(或可能是补丁)。
    <%= form_for(:product) do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% @product.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
 def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end