简体   繁体   English

Ruby on Rails:需要将下拉的ID /值传递回控制器

[英]Ruby on rails: Need to pass drop down ID/value back to the controller

I'm new to RoR and I'm trying to pass the drop down id selected by the user back to the controller to load a page specific to a business. 我是RoR的新手,我正在尝试将用户选择的下拉ID传递回控制器以加载特定于业务的页面。 I have a simple DB listing ten businesses which loads fine but when I select an option I'm getting this error: No route matches [POST] "/businesses/1/2/3/4/5/6/7/8/9/10" 我有一个简单的数据库,列出了十个可以正常加载的业务,但是当我选择一个选项时,出现此错误: 没有路由匹配[POST]“ / businesses / 1/2/3/4/5/6/7/8 / 9/10“

Can I get the action value to be dynamic for the form? 我可以使动作值对于表单是动态的吗? What are some options? 有哪些选择?

Controller: businesses_controller.rb 控制器:businesss_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

View: _header.html.erb 查看:_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 generated for the drop down: 为下拉列表生成的HTML:

<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>

Routes: 路线:

      Prefix Verb   URI Pattern                    Controller#Action
  businesses GET    /businesses(.:format)          businesses#index
             POST   /businesses(.:format)          businesses#create
new_business GET    /businesses/new(.:format)      businesses#new
edit_business GET    /businesses/:id/edit(.:format) businesses#edit
    business GET    /businesses/:id(.:format)      businesses#show
             PATCH  /businesses/:id(.:format)      businesses#update
             PUT    /businesses/:id(.:format)      businesses#update
             DELETE /businesses/:id(.:format)      businesses#destroy
        root GET    /                              businesses#home
      update GET    /update(.:format)              businesses#update

in routes.rb 在routes.rb中

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

then it is better to include action new in controller 那么最好在控制器中包含new动作

def new
  @business = Business.new
end

and use it with view new.html.erb (not home.html.erb ) to render form to submit: 并将其与view new.html.erb (不是home.html.erb )一起使用以呈现要提交的表单:

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

I was able to simplify the drop down and get the path to work: 我能够简化下拉菜单并获得工作路径:

<%= 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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM