2014-01-12 60 views
0

我是新來的RoR,我試圖將用戶選擇的下拉ID傳遞迴控制器加載特定頁面業務。我有一個簡單的數據庫列出了十個企業加載好,但當我選擇一個選項,我得到這個錯誤:沒有路線匹配[POST]「/企業/ 1/2/3/4/5/6/7/8/9/10「紅寶石軌道:需要通過下拉ID /價值返回到控制器

我可以將表單的動作值設爲動態嗎?有什麼選擇?

控制器:businesses_controller.rb

def home 
    @business_dd = Business.find(:all) 
    end 

    def update 
    @business = Business.find(params[:id]) 
    redirect_to :action => "show", :id => @business 
    end 

    def show 
    @business_dd = Business.find(:all) 
    @business = Business.find(params[:id]) 
    end 

查看:

<form accept-charset="UTF-8" action="/businesses/1/2/3/4/5/6/7/8/9/10" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="FS7sYiq8NicsKfnUwzTTHpobtA180qOjcAOnuWNONPc=" /></div>     
<select id="business_id" name="business[id]"><option value="">Please select</option> 
<option value="1">Business1</option> 
<option value="2">Business2</option> 
<option value="3">Business3</option> 
<option value="4">Business4</option> 
<option value="5">Business5</option> 
<option value="6">Business6</option> 
<option value="7">Business7</option> 
<option value="8">Business8</option> 
<option value="9">Business9</option> 
<option value="10">Business10</option></select> 
<input name="commit" type="submit" value="Submit" /> 
</form> 

路線:

的下拉產生_header.html.erb

<%= form_for :business, :url => business_path(@business_dd) do |f| %>    
     <%= f.select(:id, options_for_select(
     @business_dd.map{ |f| [f.name, f.id]}), 
     :include_blank => 'Please select') %> 
     <%= f.submit "Submit" %> 
    <% end %> 

HTML 210

回答

0
在routes.rb中

resources :businesses, only: [:allowed, :actions, :here] 

然後最好是包括動作new在控制器

def new 
    @business = Business.new 
end 

,並用它與圖new.html.erb(未home.html.erb)呈現形式提交:

<%= form_for @business do |f| %> 
    ... 
+0

實際上,我是能夠做到這一點會改變我填充下拉的方式:'<%=的form_tag(new_business_path,方法: '得到')也| F | %> \t \t \t <%= select_tag(:ID, \t \t \t options_for_select(Business.find(:所有).MAP {| F | [f.name,f.id]}), \t \t \t \t :提示=> 「選擇地點」)%> \t \t \t <%= submit_tag 「提交」 %> \t \t <% end %>' – TonyDiaz

+0

總是有幾種方法來解決任何問題。如果你使用'new_business_path',那麼你創建了'new'動作。你的問題有我的答案。如果你認爲你的答案更好 - 你可以添加並接受它。 – okliv

0

我能夠簡化下拉並獲得w獸人:

<%= form_tag(new_business_path, method: 'get') do |f| %> 
    <%= select_tag(:id, 
    options_for_select(Business.find(:all).map{ |f| [f.name, f.id]}), :prompt => "Select Venue") %> 
    <%= submit_tag "Submit" %> 
<% end %>