0
我是RoR的新手,我正在做一個小應用程序來啓動我。Ruby on Rails,將哈希值傳遞給link_to,而不使用路由中的資源
我有一個供應商頁面,其中列出所有供應商,裏面有一個link_to'新'產品頁面,我可以創建一個新產品,我需要通過link_to將供應商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途徑我得到這個:
[email protected]:~/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
EDIT3:
男y產品類別型號:
class ProductCategory < ActiveRecord::Base
attr_accessible :category
has_many :products
end
該模型的控制器爲空。
@ pireer路易Gottfrois如你所說的我做了,這是我 HTTP網址://本地主機:3000 /產品supplier_id = 2 現在我想將其存儲在產品頁面的hidden_field_tag中,但我無法弄清楚 – Kmelkon
您可以在視圖和/或控制器中使用'params'哈希來訪問URL和FORM params。 'params [:supplier_id]'會在你的情況下檢索值'2'。您可以使用'hidden_field_tag:supplier_id,params [:supplier_id]'將該值添加到隱藏字段' –
Yesss非常感謝您,它工作正常。現在我有兩個問題需要解決*創建操作不會創建產品*我需要URL在/ products/2中是乾淨的...我將產品控制器放入編輯部分..再次感謝您的支持幫助 – Kmelkon