尝试在测试

时间:2018-05-08 18:05:29

标签: ruby-on-rails ruby model-view-controller

我正在使用Ruby on Rails创建一个在线图书馆系统。目前用户可以注册,但要这样做,他们必须首先选择一种订阅计划。

最后,我发现这样做的最好方法是传递plan_id,如此:

config/routes.rb

get 'subscribe/paperback2u', to: 'members#new', :plan_id => 1
get 'subscribe/paperback4u', to: 'members#new', :plan_id => 2
get 'subscribe/paperback6u', to: 'members#new', :plan_id => 3

然后,members/new.html.erb拉出form部分,如下所示:

<%= form_with(model: member, local: true) do |form| %>
  <% plan_id = params[:plan_id] %>

  <div class="field form-group">
    <%= form.label :first_name, "First Name" %>
    <%= form.text_field :first_name, 
                        id: :member_first_name,
                        :class => "form-control",
                        :required => true %> 
  </div>

  <div class="field form-group">
    <%= form.label :last_name, "Last Name" %>
    <%= form.text_field :last_name,
                        id: :member_last_name,
                        :class => "form-control",
                        :required => true %>
  </div>

  <div class="field form-group">
    <%= form.label :address_line_1, "Address Line 1" %>
    <%= form.text_field :address_line_1,
                        id: :member_address_line_1,
                        :class => "form-control",
                        :required => true %>
  </div>

  <div class="field form-group">
    <%= form.label :address_line_2, "Address Line 2" %>
    <%= form.text_field :address_line_2,
                        id: :member_address_line_2,
                        :class => "form-control" %>
  </div>

  <div class="field form-group">
    <%= form.label :town %>
    <%= form.text_field :town,
                        id: :member_town,
                        :class => "form-control",
                        :required => true %>
  </div>

成员模型的其他字段。

然后我们到达:

 98 <%= form.hidden_field :plan_id, value: params[:plan_id] %>

100 <% if form.object.new_record? %>
101   <%= render 'stripe-form' %>
102 <% end %>

我不认为line 98正在做任何事情,说实话。

然后stripe-form部分看起来像这样:

1  <div class="text-center">
2    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button btn-primary"
3      data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
4      data-description="Subscribe to ReadAll Library"
5      data-amount=<%= (Plan.find(params["plan_id"].to_i).display_price) * 100 %>
6      data-currency="gbp"
7      data-panelLabel="Subscribe for"
8      data-label="Subscribe"
9      data-locale="auto"></script>
10 </div>

我得到的错误是:

Error:
MembersControllerTest#test_should_get_new:
ActionView::Template::Error: Couldn't find Plan with 'id'=1
    app/views/members/_stripe-form.html.erb:5:in `_app_views_members__stripe_form_html_erb___374722773566915779_70180218242520'
    app/views/members/_form.html.erb:101:in `block in _app_views_members__form_html_erb__2640496602018942691_70180179508500'
    app/views/members/_form.html.erb:1:in `_app_views_members__form_html_erb__2640496602018942691_70180179508500'
    app/views/members/new.html.erb:5:in `_app_views_members_new_html_erb___3025732295195500162_70180218177380'
    test/controllers/members_controller_test.rb:16:in `block in <class:MembersControllerTest>'

如果我在测试中添加puts response.body,我得到的错误是:“计划必须存在”。它做到了!我的plans表中有3个不同的计划。每个人都有一个ID,当我实际运行它时一切正常。这只是测试不起作用。如果可以的话,我会废弃测试,但我需要它作为项目的文档。

最后,这是我的Plan.rbMember.rb

`plan.rb`

 class Plan < ApplicationRecord
   has_many :members
 end

`member.rb`

   class Member < User
1    require 'stripe'
2 
3    before_save :set_type
4 
5    belongs_to :plan
6 
7 
8    def set_type
9      self.type = "Member"
10   end
11
12 end

两者的架构:

create_table "plans", force: :cascade do |t|
  t.string "stripe_id"
  t.string "nickname"
  t.decimal "display_price"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "product"
end

create_table "users", force: :cascade do |t|
  bunch of user attributes (first_name, address, etc)
  t.string "type" (members is a users type)
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "remember_digest"
  t.string "customer_id"
  t.bigint "plan_id"
  t.index ["email"], name: "index_users_on_email", unique: true
  t.index ["plan_id"], name: "index_users_on_plan_id"
end

我没有想法。我感谢你们给我的任何帮助。欢呼声。

1 个答案:

答案 0 :(得分:0)

在您看来,如果您希望用户从表单中进行选择,您可以添加选择输入并让用户选择他们想要的计划。但是,您可以通过URL传递此信息,就像您已经做过的那样。

查看:

<%= form_with(model: member, local: true) do |form| %>

  <div class="field form-group">
    <%= form.label :first_name, "First Name" %>
    <%= form.text_field :first_name, 
                        id: :member_first_name,
                        :class => "form-control",
                        :required => true %> 
  </div>

  <div class="field form-group">
    <%= form.label :plan, "Choose your plan" %>
    <%= form.select :plan_id, 
        Plan.all {|p| [ p.name, p.id ]}, 
        { include_blank: false, selected: form.object.plan_id }, class: 'form-control' %>
  </div>

  <%= f.submit "Salvar", class: "btn btn-primary" %>  
<% end %>

我相信其余的你已经做到了。在控制器中,您可以在plan_id中添加params的白名单,并在模型中添加模型关系as_many并属于。 P.s:我没有测试过这个。