条带化表单的嵌套资源表单

时间:2015-04-29 18:06:58

标签: ruby-on-rails ruby-on-rails-4 form-for nested-routes

我需要了解如何为嵌套路由设置form_for,以便我可以在隐藏字段中传递条带标记。对于我已阅读的文档并查看了我的form_for设置看起来正确但我在开始form_for的行上收到此错误:

    undefined method `membership_program_path' for #<#<Class:0x007fd4ef3da588>:0x007fd4f34ccf28>

这是我的路线:

      resources :programs, except: [:show] do
      resources :memberships, only: [:new, :create]
      end

我的会员控制员:

      def new
      @membership = Membership.new
      @program = Program.find(params[:program_id])
      end

这是我的整个_form.html.erb

     <%= form_for [@membership, @program] do |f| %>
     <%= f.hidden_field :stripe_card_token %>

     <div class="field">
     <%= f.label :email %>
     <%= f.text_field :email %>
     </div>

     <% if @members.stripe_card_token.present? %>
     Credit card has been provided.
     <% else %>
     <div class="field">
     <%= label_tag :card_number, "Credit Card Number" %>
     <%= text_field_tag :card_number, nil, name: nil %>
     </div>
    <div class="field">
    <%= label_tag :card_code, "Security Code on Card (CVV)" %>
    <%= text_field_tag :card_code, nil, name: nil %>
    </div>
    <div class="field">
    <%= label_tag :card_month, "Card Expiration" %>
    <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
    <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
    </div>
   <% end %>
   <div id="stripe_error">
     <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
   </div>
   <%= f.submit %>
  <% end %>

1 个答案:

答案 0 :(得分:0)

错误的原因是因为我的资源设置为“会员资格”嵌套在“程序”下;但是,调用form_for(@membership, @program)告诉form_for您的程序嵌套在成员资格下。因此,为什么它会查找不存在的membership_program_path。我颠倒了传递给form_for的参数的顺序,并且它有效。