Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6801289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:04:55+00:00 2026-05-26T19:04:55+00:00

I have Bands and Codes and Users. the key/codes will let a user do

  • 0

I have Bands and Codes and Users.

the key/codes will let a user do things with the band. I am on the Band#show action and need to generate a code, and then have that code be related to the user and the band.

so:

Code belongs_to :band, belongs_to :user
Band has_many :codes
User has_many :codes

So now I need to use a form_for (I think) to create a button which when pressed does the following:

check if a user has a code with that band(user can have one code per band)

If not:

  • Creates a 6 digit alphanumeric key/code (I know ActiveSupport can do
    it: <%= SecureRandom.hex(3)%> but this needs to be in the model or
    controller)
  • Set the correct associations, this code works for this user and this band. I am using Devise so I have access to a current_user method
  • and return the user to the Band#show
    action

If the user already has a code for that band, then they are given an error “You already have a code”

I want to eventually make this call with Ajax to keep from reloading the page but for now I am just trying to build the function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:04:55+00:00Added an answer on May 26, 2026 at 7:04 pm

    First off, I would create the following route in your routes.rb:

    resources :bands do
      post :add_code, :on => :member
    end
    

    This will create an additional route looking like this:

    add_code_band_path POST   /bands/:id/add_code(.:format)  {:action=>"add_code", :controller=>"bands"}
    

    Then from your views you can use the button_to helper method to send a post request like this:

    <%= button_to "Add Code", add_code_band_path(@band), :method => :post %>
    

    When the user clicks on that button then you can handle the rest from the controller action like this:

    class BandsController < ApplicationController
      ...
    
      def add_code
        @band = Band.find(params[:id])
        @code = current_user.codes.find_or_create_by_band_id(@band.id)
        render :action => :show
      end
    

    Lastly, the actual randomized code I would generate from a before_create callback from the Code model itself, like this:

    class Code < ActiveRecord::Base
      ...
    
      before_create :generate_code
    
    private
      def generate_code
        self.code = SecureRandom.hex(3)
      end
    end
    

    Then if you think it’s necessary then you could also add other verifications like only showing the Add Code button if there is no Code existing or perhaps add error messages if a user tries to add another code for the same band, but I leave that to you. The button_to helper also supports Ajax by using :remote => true so you can further develop it the way you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Users Bands and Codes. A user creates a code and that code
I have Users, Bands and Codes Bands have many Codes, Users have many codes,
I have the following C# code which logs the user's url in a cookie.
I have a dashboard page where the user can enter an asset code or
I have inherited some Silverlight\WCF code and now need to implement some new features.
I have a 4x4 matrix which I wish to decompose into 4 frequency bands
The instruments that are used with Guitar Hero and Rock Band have USB connections.
i have created an IE band object (toolbar) that is working well. however, when
I have a WPF User control that binds to a DataTable and generates CheckBox
I have a strange issue from a client in that our code, which they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.