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 8882405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:34:12+00:00 2026-06-14T20:34:12+00:00

I have the following action in my tags controller: ( params[:q] comes from this

  • 0

I have the following action in my tags controller:
(params[:q] comes from this plugin http://loopj.com/jquery-tokeninput/)

  def index
    @tags = Tag.where("name like ?", "%#{params[:q]}%")
    results = @tags.map(&:attributes)
    @tag = Tag.find_by_name(params[:q])
    if @tag.blank?
      results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"}
    end

    respond_to do |format|
      format.html
      format.json { render :json => results }
    end
  end

This part:

  @tag = Tag.find_by_name(params[:q])
  if @tag.blank?
    results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"}
  end

Makes the line of code above to only trigger if the current tag name doesn’t exist.

The problem is that if the user types something like this in the input:

"programming " <- a trailing white space
" programming" <- a trailing white space

the code is executed anyway, since the trailing spaces makes Rails think that it is a new name.

what’s the best way of dealing with this problem?

  • 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-06-14T20:34:13+00:00Added an answer on June 14, 2026 at 8:34 pm

    You could do:

    params[:q].delete!(' ')
    @tag = Tag.find_by_name(params[:q])
    if @tag.blank?
      results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"}
    end
    

    Note the first line will delete all spaces in the params[:q]. If this is not what you want, you can first duplicate the variable with dup method

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

Sidebar

Related Questions

I have the following controller action: [HttpGet] public JsonpResult getTestValues(int? entityId, int? id) {
I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string
So, I have a page that looks like the following: alt text http://img704.imageshack.us/img704/5973/croppercapture3.png This
Supposing I have the following action: def index @posts = Post.joins(:tags).where(:tags => {:id =>
I have the following action methods: public ActionResult ProfileSettings() { Context con = new
I have the following action method: [HttpGet, Authorize, OutputCache(Duration = 60, VaryByHeader = Cookie,
I have the following action, appointments_for_week : class StylistsController < ApplicationController def appointments_for_week stylist
I have the following action in my ASP.NET Web API: public IEnumerable<Car> carssOfUser(int id,
I have the following index action: class ExpensesController < ApplicationController def index() @expenses =
Okay, I have the following create action #posts_controller, nested resource under discussions def create

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.