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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:47:25+00:00 2026-06-11T11:47:25+00:00

In my controller I have: def search @kategoris = Kampagner.where(titel like ?, %#{params[:q]}%) @kate

  • 0

In my controller I have:

def search
  @kategoris = Kampagner.where("titel like ?", "%#{params[:q]}%")
  @kate = []
  @kategoris.each do |kat|
    h = {}
    kat.attributes.each{|k,v| h[k] = v.respond_to?(:force_encoding) ? v.dup.force_encoding("UTF-8") : v }
    @kate << h
  end
  respond_to do |format|
  format.html
  format.json { render :json => @kate }
  end
end

But the problem is just that all of the attributes for the model are in the JSON data. I only what the attributes ID and title to be in the JSON data. How do I select this?

  • 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-11T11:47:26+00:00Added an answer on June 11, 2026 at 11:47 am

    I’m not very clear why you want to use force_encoding. But you can simply just call:

    format.json { render :json => @kategoris }
    

    Rails will call the method as_json behind the scene. Then in the Kampagner class you can customize the as_json class to control what will be exposed when the record was exported to JSON:

    class Kampagner
      def as_json(options={})
        super(options.merge({ :only => [:id, :title]})
      end
    end
    

    See more: http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html

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

Sidebar

Related Questions

I have a controller method like such: def search = { def query =
In my controller I have: @taletids = Taletid.where(:online => true) @taletids.each{|taletid| taletid.calculate_total(params[:search])} @taletids.sort! In
I have in my controller this: def boards @user = User.find_by_slug(params[:id]) @search = @user.boards.solr_search
Suppose I have a controller method like so: @expose() def search(self, title): return dict()
In my Rails controller I have something like: def authenticate_user! if not current_user #
I have a controller returning a json structure like so: def show # .......
I have a controller like this : @Secured(['ROLE_USER','IS_AUTHENTICATED_FULLY']) def userprofile(){ def user = User.get(springSecurityService.principal.id)
I have a controller method that is a little something like this: def suggestions
I have two actions in my controller : def find @item = Item.find(params[:id]) render
In my controller action, I have the following: def index @articles = (params[:mine] ==

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.