2013-06-24 29 views
0

我有rails form_tag helper來保存do..each循環提供的數據。這是我的形式:Rails:如何從字符串中保存少量元素

  <%= form_tag (customers_path) do |f| %> 
       < @contacts.each do |c|%> 
        <%= check_box_tag "accept[]", c %><%= c[:email] %> 
       <% end %> 
       <%= submit_tag ("Save") %> 
      <%end%> 

此表格保存其複選框被選中的聯繫人。這是什麼,C有:

"accept"=>["{:id=>\"2f310f1d9b8f\", 
:first_name=>\"San\", 
    :last_name=>\"Jori\", 
    :name=>\"Jori,San\", 
    :email=>\"[email protected]\", 
    :gender=>nil, 
    :birthday=>nil, 
    :profile_picture=>nil, 
    :relation=>nil}"], 
    "commit"=>"Save" 

我只想保存:名稱和:從上面的哈希電子郵件。 這是我的控制器創建行動:

if params[:accept].present? 
    params[:accept].each do |customer| 
     @customer = current_user.customers.new(:name => customer[:name], :email => customer[:email]) 
    @customer.save 
    end 
    redirect_to customers_path 
    end 

但它給人的錯誤:

no implicit conversion of Symbol into Integer 

誰能告訴我怎麼做工作?

謝謝!

回答

0

而不是使用

@customer = current_user.customers.new(:name => customer[:name], :email => customer[:email]) 

嘗試

+0

u必須給顯示器O/P '姓名' 和 '電子郵件' 不是在它的值的解決方案。它應該顯示Jori,San,[email protected]。 – user2206724

+0

我想在不同的層次發生錯誤,當您在每個區塊內添加「p客戶」時會發生什麼? –

+0

它給出了這個:{:id =>「112f310f1d9b8f」,:first_name =>「San」,:last_name =>「Jori」,:name =>「Jori,San」,:email =>「[email protected] 「,:gender => nil,:birthday => nil,:profile_picture => nil,:relation => – user2206724

相關問題