为什么参数被传递为“ /”?

时间:2018-08-02 10:50:22

标签: ruby-on-rails ruby

我有一个产品和类别控制器,当我创建一个新产品时,:category_id被传递为“ /”。我不知道为什么。 Routes.rb很好,模型是标准的,controller#create没问题(在下面发布)。怎么了?

def new
    @product = Product.new
  end


def create
    @product = Product.new(product_params)
    @product.category_id = params[:category_id]


    respond_to do |format|
      if @product.save
        format.html { redirect_to category_products_path(@category), notice: 'P$
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_enti$
      end
    end
  end

_form.html.erb

<%= form_for [@category, @product], url: category_products_path([@category, @product]) do |form| %>
  <% 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">
    <%= form.label :name %>
    <%= form.text_field :name %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

耙道:

 category_products GET    /categories/:category_id/products(.:format)                                              products#index
                        POST   /categories/:category_id/products(.:format)                                              products#create
   new_category_product GET    /categories/:category_id/products/new(.:format)                                          products#new
  edit_category_product GET    /categories/:category_id/products/:id/edit(.:format)                                     products#edit
       category_product GET    /categories/:category_id/products/:id(.:format)                                          products#show
                        PATCH  /categories/:category_id/products/:id(.:format)                                          products#update
                        PUT    /categories/:category_id/products/:id(.:format)                                          products#update
                        DELETE /categories/:category_id/products/:id(.:format)                                          products#destroy

2 个答案:

答案 0 :(得分:1)

category_products_path([@category, Product.new]) #=> "categories/1%2f/products"

其中%2f的{​​{1}}代码。 /是一个数组,并作为单个参数传递。

[@category, Product.new]

每个都可以。

此外,未设置<%= form_for [@category, @product], url: category_products_path(@category, @product) do |form| %> <% end %> <%= form_for [@category, @product], url: category_products_path(@category) do |form| %> <% end %> <%= form_for [@category, @product], url: category_products_path(@category.id) do |form| %> <% end %> <%= form_for [@category, @product], url: category_products_path(category_id: @category.id) do |form| %> <% end %> redirect_to category_products_path(@category)之前。因此,将当前路径更改为: @category

并更新您的操作redirect_to category_products_path(@product.category_id)

new

答案 1 :(得分:0)

<%= form_for Product.new, url: category_products_path(params[:category_id]) do |form| %>
  <% 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">
    <%= form.label :name %>
    <%= form.text_field :name %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

控制器:-

def create
  @product = Product.new(product_params)
  @product.category_id = params[:category_id]
    respond_to do |format|
      if @product.save
        format.html { redirect_to category_products_path(@product.category), notice: 'P$
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_enti$
      end
    end
end