2015-07-19 55 views
1

我要轉移的:行動是用戶選擇到下一個頁面如何傳輸select_tag參數button_to

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> 

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

RecordController

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 

的ACCOUNT_NAME成功

發送,但其類型變得字「型」,而不是用戶選擇

如何轉移:用戶類型選擇?

+0

用表格把它包起來,並使用提交.. – Nithin

回答

1

所以,你要到action選擇的選項在指數中的type新的窗體上轉移?

index.html.erb

<td> 
    <%= form_for(new_record_path(account_name: account.name), class: "btn btn-primary") do |f| %> 
    # Not sure you need to specify method: :get, as it is already defined in your routes when you wrote `resources: :records` 
    <%= select_tag :action, options_for_select(actions[account.type]) %> 
    <%= submit_tag "execute", class: "btn btn-primary" %> 
<% end %> 

然後在你的new.html.erb

<%= form_for @record do |f| %> 
    <%= f.hidden_field :type, value: params[:action] %> 
    <%= 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 %>` 
+0

感謝您的幫助 但ACCOUNT_NAME沒有轉移到 '新' 頁面 最後,我用下面的方法 ​​<%=的form_tag(new_record_path,方法: '得到')辦%> <%= hidden_​​field_tag:account_name,account.name%> <%= select_tag:type,options_for_select(actions [account.type])%> <%= submit_tag「執行動作」,class:「btn btn-primary」%> <% end %> –

1

試試這個

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