Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't want to muck up your user model with the promocode attribute, I think you can use the <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001731" rel="nofollow noreferrer"><code>text_field_tag</code></a> (and corresponding [<code>label_tag</code>]) from within your <code>form_for</code>. Something like:</p> <pre><code>&lt;% form_for @user do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;%= label_tag "promo_key" %&gt; &lt;%= text_field_tag "promo_key" %&gt; &lt;%= f.label :login, 'Login' %&gt; &lt;%= f.text_field :login %&gt; &lt;%# ...other fields go here... %&gt; &lt;%= f.submit 'Signup!' %&gt; &lt;% end %&gt; </code></pre> <p>Then, in your controller's create action, you can simply access the entered promo code by checking <code>params[:promo_code]</code>.</p> <pre><code>def create @user = User.new(params[:user]) #build up the user object promo_code = PromoCodes.find_by_name(params[:promo_code]) #find the promo code that user entered if promo_code.allow_reg == 0 #make sure that the code has uses left flash[:notice] = "That promo code has expired. Whoops!" render :action =&gt; 'new' else if @user.save #i assume you want to decrement the allow_reg on a successful registration? promo_code.update_attribute :allow_reg, promo_code.allow_reg - 1 flash[:notice] = "User account created! Please log in with your new account" redirect_to login_url else render :action =&gt; 'new' #user couldn't be saved. validation error? end end end </code></pre> <p>Would that work too? (I'd probably consider moving the promocode checking out of the create action and into a before filter, to keep things cleaner.)</p> <p>-John</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload