开发者

Store input from text field into a different table than the primary form builder model? - Rails3

开发者 https://www.devze.com 2023-03-30 03:41 出处:网络
TL;DR I want to check my Coupon table when a new user registers to see if that Coupon Code exists. If it does, apply the appropriate discount i.e. coupon.discount_amount to that new user\'s account.

TL;DR

I want to check my Coupon table when a new user registers to see if that Coupon Code exists. If it does, apply the appropriate discount i.e. coupon.discount_amount to that new user's account. But this is just on one field in the new user registration form (the form which belongs to the User model).

Long Version

I am using Devise to manage my User registration and logins.

So this is what my registration/new/index.html.erb view looks like:

<% @user.plan_id = params[:plan_id] %>

    <%= devise_error_messages! %><br />

    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>

        <% if !params[:promo] %>
        <%= f.collection_select :plan_id, @plan, :id, :display_name, :prompt => "Choose yourself up a real nice plan" %><br />
        <% end %>
        <%= f.text_field :username, :placeholder => "Desired Username" %><br />
        <%= f.text_field :first_name, :placeholder => "First Name" %><br />
        <%= f.text_field :last_name, :placeholder => "Last Name" %><br />
        <%= f.password_field :password, :placeholder => "Password" %><br />
        <%= f.password_field :password_confirmation, :placeholder => "Password" %><br />
        <%= f.text_field :email, :placeholder => "Email Address" %><br />
        <% if params[:promo] %>
            <%= f.text_field :xcode, :placeholder => "Coupon Code" %><br /> 
        <% end %>
    开发者_运维知识库    <div class="settings_button">
            <%= f.submit "Sign in", :id => "register", :value => "Register", :class => "pill button" %>
        </div>

<% end %>

The line in question is the line that says <%= f.text_field :xcode, :placeholder => "Coupon Code" %><br />.

Every other field corresponds to a column in my User model...but this one should correspond to an association that's pretty complex.

For instance, say test = User.find('test').

Then the Coupon_Code entered by Test, would be stored in test.subscriptions.last.coupon.code.

So I want to do two things...when Test enters coupon code: 4gHu7k, I want to do a Coupon.find('4gHu7k'). If it does exist, then I want to set the following (assuming that this new coupon would have an ID of 5): test.subscriptions.last.coupon_id.

The schema for my Subscription model is as follows:

class Subscription < ActiveRecord::Base
    belongs_to  :plan
    has_one :coupon
end


# == Schema Information
#
# Table name: subscriptions
#
#  id                   :integer         not null, primary key
#  status               :string(255)
#  plan_id              :integer
#  payment_method_token :string(255)
#  created_at           :datetime
#  updated_at           :datetime
#  user_id              :integer
#  period_start         :datetime
#  period_end           :datetime
#  coupon_id            :integer
#

I hope I have explained what I need clearly, let me know if you need any more info.

Thanks.

P.S. When I run it as is, I get the following:

undefined method `xcode' for #<User:0x00000103583d70>

Which is around that line.


I would suggest instead of using f.text_field, why dont you try text_field_tag? <%= text_field_tag "sample" %>

Now you can access this value in controller as params[:sample]. Hope this helps.

Cheers.

0

精彩评论

暂无评论...
验证码 换一张
取 消