我需要表單的選定值以某種方式傳遞給我的控制器。我不確定我在路線和控制器中放置什麼來獲得價值。rails,如何將我的collection_select值傳遞給控制器
show.html.erb
<h1>Report#show</h1>
<%= form_tag new_report_path(:cname) do %>
<label for="lookup">Lookup</label>
<%= collection_select(:cname, 2, Company.all, :id, :name) %>
<%= submit_tag "Submit" %>
<% end %>
控制器report_controller.rb
def show
end
def add
# how do i get the :cname here?
end
路線
root 'report#show'
get '/report/index' => 'report#show'
我測試了什麼? 我測試了我的代碼,它給了我這個URL。
當前結果:http://localhost:3000/report/new.cname
預期結果:http://localhost:3000/report/:cname
******** UPDATE ******
我測試了widjajayd的解決方案。 它返回此錯誤回到我.. 我也只是提供控制器名稱這裏
這是我耙路線以及
add_reports POST /reports/add(.:format) reports#add reports GET /reports(.:format) reports#index POST /reports(.:format) reports#create new_report GET /reports/new(.:format) reports#new edit_report GET /reports/:id/edit(.:format) reports#edit report GET /reports/:id(.:format) reports#show PATCH /reports/:id(.:format) reports#update PUT /reports/:id(.:format) reports#update DELETE /reports/:id(.:format) reports#destroy company_index GET /company(.:format) company#index POST /company(.:format) company#create new_company GET /company/new(.:format) company#new edit_company GET /company/:id/edit(.:format) company#edit company GET /company/:id(.:format) company#show PATCH /company/:id(.:format) company#update PUT /company/:id(.:format) company#update DELETE /company/:id(.:format) company#destroy root GET / report#show
你的鏈接將無法工作,因爲它是一個'localhos t'! – Pavan
http://guides.rubyonrails.org/routing.html – Pavan
它顯然不會工作......我還沒有解僱它...... @Pavan – Napmi