将多个选定参数传递给提交链接

时间:2012-09-10 01:23:16

标签: ruby-on-rails ruby

我有一个尝试自动提交到后端的设置。有3个不同的下拉框,其中包含用户可选择的参数。我设置了这样的网站(所有'脚手架'):

class Xsearch < ActiveRecord::Base
  has_many :xentry
  has_many :spectrafile
  has_many :parameter
end

每个条目/文件/参数在其模型中都有belongs_to:xsearch。

xsearch的index.html.erb具有以下内容:

<h1>Spectra Submitter</h1>
<h2>Select a database to search</h2>
<%= collection_select("xpost", :id, Xentry.all(), :title, :name ) %>
<h2>Select a parameter file to use</h2>
<%= collection_select(:ppost, :id, Parameter.all(), :name, :name ) %>
<h2>Select a Spectra file (or folder) to search</h2>
<%= collection_select(:spost, :id, Spectrafile.all(), :name, :name ) %>
<%= link_to "Search", :controller => "xsearches", :action => "search" %>

我也尝试过使用submit_tag。我不知道如何在我的控制器中触发一个方法(如果它应该在控制器中?因为它来自我假设放在这里的浏览器),它具有来自每个collection_select的选定值。

简而言之:如何为每个collection_select选择所选值并将它们传递给函数?该功能应放在何处(在控制器或.html.erb文件中)?

谢谢,这让我发疯了。

1 个答案:

答案 0 :(得分:0)

我最终通过转移到form_new方法来解决问题。这是我搜索的其他人的代码。此脚本的目的是允许其他用户通过授权用户向群集提交搜索。

<%= form_for(@xsearch) do |f| %>
  <% if @xsearch.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@xsearch.errors.count, "error") %> prohibited this xsearch from being saved:</h2>

      <ul>
      <% @xsearch.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
    <div class="field">
      <h2>Pick A Database To Search</h2>
      <%= collection_select(:db, :id, @all_entries, :id, :name) %>
      <h2>Pick A Parameter File To Use</h2>
      <%= collection_select(:para, :id, @all_parameters, :id, :name) %>
      <h2>Pick a File or Folder To Search</h2>
      <%= collection_select(:sfile, :id, @all_spectrafiles, :id, :name) %>
    </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

控制器中的相关位创建:方法

def create
  @database = Xentry.find(params[:db][:id])
  @paramfile = Parameter.find(params[:para][:id])
  @spectrafile = Spectrafile.find(params[:sfile][:id])
  @recurse = params[:recurse]
  @email = params[:emailtag]
  if @email.empty?
    @email = "None"
  end
  if (@recurse == 'Yes')
    @recurse = 1
  else
    @recurse = 0
  end
  @recurse = 0#disabled until kill methods are implemented
  @hostname = hostname
  @username = username
  Net::SSH.start(@hostname, @username) do |session|
    session.exec('source .bashrc')
    session.exec('sh rscript.sh %s %s %s %s %d' % [@database['name'], @spectrafile['name'], @paramfile['name'], @email, @recurse])
    @xsearch = Xsearch.new(:database => @database['name'], :parameters => @paramfile['name'], :spectra => @spectrafile['name'])
    respond_to do |format|
      if @xsearch.save
        format.html { redirect_to @xsearch, notice: 'Search was successfully created.' }
        format.json { render json: @xsearch, status: :created, location: @xsearch }
      else
        format.html { render action: "new" }
        format.json { render json: @xsearch.errors, status: :unprocessable_entity }
      end
    end
  end
end