Ruby on rails 如何将select_标记参数传递到按钮

Ruby on rails 如何将select_标记参数传递到按钮,ruby-on-rails,Ruby On Rails,我想将用户选择的:操作转移到下一页 index.html.erb <td><%= select_tag :action, options_for_select(actions[account.type])%> <%= button_to "execute", {:controller => "records", :action => "new", :type => :action, :account_name => account.name}

我想将用户选择的:操作转移到下一页

index.html.erb

<td><%= select_tag :action, options_for_select(actions[account.type])%>
<%= button_to "execute", {:controller => "records", :action => "new", :type => :action, :account_name => account.name}, class: "btn btn-primary" %></td>

“记录”,:操作=>“新建”,:类型=>:操作,:帐户\u名称=>account.name},类:“btn btn主”%>
new.html.erb

<%= form_for @record do |f| %>
  <%= f.hidden_field :type, value: :type %>
  <%= f.hidden_field :account_name, value: params[:account_name] %>

  <div class="actions">
    <%= f.submit "Submit", class: "btn btn-primary" %>
  </div>
<% end %>

记录控制器

class RecordsController < ApplicationController
  def index
    @records = Record.all
  end

  def new
    @record = current_user.records.build
  end

  def create
    @record = current_user.records.build(create_params)
    if @record.save
        flash[:success] = "Posted successfully"
        redirect_to('/root/tally_book')
    else
        redirect_to('/root/tally_book')
    end
end

def destroy
    @record = current_user.records.find_by(id: params[:id])
    if @record && @record.destroy
        flash[:success] = "Post deleted"
    else
        flash[:error] = "Cannot delete post"
    end
        redirect_to('/root/tally_book')
end

private

def create_params
    params.require(:record).permit(:account_name, :description, :usage, :type, :sum, :receipt)
end
end
类记录控制器
无法成功发送帐户名

但类型将变为单词“type”,而不是用户选择的

如何传输:用户选择的类型?

试试这个

<%= form_tag(new_record_path(account_name: account.name), method: 'get' do %>
  <%= select_tag "action", options_for_select(actions[account.type]) %>
  <%= submit_tag "execute", class: "btn btn-primary" %>
<% end %>

那么您想将索引中的
操作
所选选项转移为新表单上的
类型

index.html.erb

<td><%= select_tag :action, options_for_select(actions[account.type])%>
<%= button_to "execute", {:controller => "records", :action => "new", :type => :action, :account_name => account.name}, class: "btn btn-primary" %></td>

#不确定是否需要指定method::get,因为在编写`resources::records'时,它已在路由中定义`
然后在新的.html.erb中

<%= form_for @record do |f| %>
  <%= f.hidden_field :type, value: :type %>
  <%= f.hidden_field :account_name, value: params[:account_name] %>

  <div class="actions">
    <%= f.submit "Submit", class: "btn btn-primary" %>
  </div>
<% end %>

或者,你可以在你的控制器里做这些事情

def new
  @type = params[:action] ? params[:action] : DEFAULT_VALUE
end

and use `<%= f.hidden_field :type, value: @type %>`
def新建
@类型=参数[:操作]?参数[:操作]:默认值
结束
和使用``

用一个表单将其包装起来,然后使用submit。谢谢您的帮助,但帐户名不会转移到“新建”页面。最后,我使用以下方法