0

我是 RoR 的新手,我正在做一个小应用程序只是为了让我开始。

我有一个供应商页面,其中列出了所有供应商,并且在其中我有一个链接到“新”产品页面,我可以在其中创建新产品,我需要通过链接到产品页面将供应商 ID 发送到产品页面并存储在hidden_​​field_tag 值

这是我的产品页面

<%= form_for @product, :url => {:action => "create"} do |f| %>

        <%= f.label :product_name %>
        <%= f.text_field :name %>



        <%= collection_select(:product, :product_category_id, ProductCategory.all, :id, :category,  {prompt: 'Select category'}) %>

        <%= hidden_field_tag(:supplier_id, Supplier.first[:id]) %>

        <%= f.submit "Create a product", class: "btn btn-primary"%>

    <% end %>`

Supplier.first[:id] 只是为了尝试代码

这是我的供应商页面

<% provide(:title, 'All suppliers') %>
<h1>All suppliers</h1>
<ul class="suppliers">
    <% @suppliers.each do |supplier| %>
    <li>
        <%= link_to supplier.name %>
        <%= link_to "Add a product", products_path, :class => "btn btn-primary",:method => "get" %>

        <div>
            <% if supplier.products.any? %>
            <h5>Products (<%= supplier.products.count %>)</h5>
            <ol>
            <% supplier.products.each do |product| %>

                <li><%= product.name %></li>
                <% end %>
            </ol>
            <% end %>
        </div>
    </li>
    <% end %>
</ul>

有任何想法吗 ?

编辑:

class ProductsController < ApplicationController



    def new
        @product = Product.new

    end

    def create

        @product = Product.new(params[:product])
        if @product.save
            redirect_to "/products"
        else
            render 'new'
        end
    end

编辑2:我的产品型号:

class Product < ActiveRecord::Base
  attr_accessible :name, :supplier_id, :product_category_id

  attr_accessor :supplier_id, :product_category_id


  belongs_to :supplier

  belongs_to :product_category

  validates :supplier_id, presence: true
  validates :product_category_id, presence: true

end

当我运行 rake 路线时,我得到了这个:

karam@karam-Inspiron-N5010:~/rails-projects/devise$ rake routes
       new_admin_session GET    /admins/sign_in(.:format)      devise/sessions#new
           admin_session POST   /admins/sign_in(.:format)      devise/sessions#create
   destroy_admin_session DELETE /admins/sign_out(.:format)     devise/sessions#destroy
            admin_unlock POST   /admins/unlock(.:format)       devise/unlocks#create
        new_admin_unlock GET    /admins/unlock/new(.:format)   devise/unlocks#new
                         GET    /admins/unlock(.:format)       devise/unlocks#show
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                products POST   /products(.:format)            products#create
             new_product GET    /products/new(.:format)        products#new
                 product GET    /products/:id(.:format)        products#show
           suppliers_new        /suppliers/new(.:format)       suppliers#new
               suppliers        /suppliers(.:format)           suppliers#index
        suppliers_create        /suppliers/create(.:format)    suppliers#create
                                /products(.:format)            products#new

编辑3:

我的 ProductCategory 模型:

class ProductCategory < ActiveRecord::Base
  attr_accessible :category

  has_many :products


end

这个模型的控制器是空的。

4

1 回答 1

0

只需这样做:

<%= link_to "Add a product", products_path(supplier_id: supplier.id), :class => "btn btn-primary" %>

(您不需要:method => "get",因为link_to默认情况下是 GET)

此外,以下内容不正确:

<%= link_to supplier.name %>

改为这样做:

<%= link_to supplier.name, supplier_path(supplier) %>

您应该按如下方式清洁控制器:

class ProductsController < ApplicationController
  def new
    @product = Product.new
  end

  def create
    @product = Product.new(params[:product])
    if @product.save
      redirect_to products_path
    else
      render :new
    end
  end
end
于 2013-07-02T14:59:57.310 回答