I am unable to edit or add a new model in activeadmin that has an HStore column due to the following error.
Unable to find input class for hstore
How can I get activeadmin and formtastic to play nice with my HStore column? This question has been asked but I cannot find a definitive answer that allows for adding and editing of the hstore field.
I have used these references so far
With latest activeadmin (which uses ransack instead of meta_search) it's possible to define a custom ransacker for hstore field in a model:
class Room < ActiveRecord::Base
store_accessor :options, :amenities
ransacker :amenities do |parent|
Arel::Nodes::InfixOperation.new('->', parent.table[:options], 'amenities')
end
end
Then it can be used in activeadmin for filtering:
ActiveAdmin.register Room do
filter :amenities_eq, label: 'Amenities', as: :select # ...
end
Filter activeadmin with hstore
and
https://github.com/gregbell/active_admin/issues/2032
For anyone who happens to stumble across this, I was able to use the activeadmin_hstore_editor gem for this purpose, which gives a way to input arbitrary json into an input field.
Related
I am using Grape + Mongoid + Devise.
I found that the Devise user model have more fields (e.g. encrypted_password, sign_in_count, last_sign_in_at) than the user json output when I wrote API response.
I have searched in Devise code, didn't find anything like custom to_json, how does Devise achieve that?
I'm not sure on Grape, but on Rails you can do it with a serializer (as Grape has many code compatible with Rails, I think there's a big chance to work).
To use a serializer, you need to include the "active_model_serializers" gem.
Example:
class UserSerializer < ActiveModel::Serializer
attributes :id, :email, :username
end
On this example, Devise will always print only these 3 fields on a JSON output.
To include all attributes except some of them, you can do something like this:
class UserSerializer < ActiveModel::Serializer
attributes(*(User.attribute_names - ["date_created", "first_name"] ).map(&:to_sym))
end
Also, at least on Rails, you'll want to remove the root from the output. To do this, add this code to your application_controller.rb:
def default_serializer_options
{root: false}
end
I have a rails 3 app where I am using acts_as_taggable_on to tag a Post model. I have a search box which allows me to search through the posts, where I use acts_as_indexed. In my search, I want to also be able to search through all the tags, by extending the ActsAsTaggableOn::Tag class to support acts_as_indexed. How do I do that?
I tried the following:
# lib/tag.rb
module ActsAsTaggableOn
class Tag
acts_as_indexed :fields => [:name]
end
end
but then I get the following error when I try to call find_with_index:
> ActsAsTaggableOn::Tag.find_with_index('foo')
undefined method `find_with_index' for ActsAsTaggableOn::Tag(id: integer, name: string):Class
UPDATE: I made this work using acts_as_taggable_on's built in finders (specifically, Tag#named_like), but I would prefer to use acts_as_indexed, since I use it with all the other models in my app, and I am having to special case tag searching due to this issue.
Thanks,
It might be easier to extend the act_as_indexed declaration on the Post model so that tags related to each post are included in a search.
class Post
acts_as_taggable_on
acts_as_indexed :fields => [ <existing tag list> , :tag_list]
I want to adding tagging facility in my application . so, I am using acts_as_taggable_on : https://github.com/mbleigh/acts-as-taggable-on
I have added following line my Gemfile:
gem 'acts-as-taggable-on', '~> 2.2.2'
and when I add following line in my user model:
acts_as_taggable_on
It gives me this error:
undefined local variable or method `acts_as_taggable_on' for User:Class
Kindly, tell me what am I doing wrong?
That gem isn't going to work with mongoid and mongodb because it is built to allow tagging using a relational database using active record.
The good news is that this is very simple to do in mongoid. Simply add a new Array field named after whatever you would have listed as being acts_as_taggable_on. If you also have acts_as_taggable, include a generic tags field as well.
If you were going to have a model that looked like this:
class User < ActiveRecord::Base
acts_as_taggable
acts_as_taggable_on :skills, :interests
end
You would build it like this with mongoid:
class User
include Mongoid::Document
field :tags, type: Array
field :skills, type: Array
field :interests, type: Array
end
Then when you wanted to save a tag, lets say as an interest you would do the following:
#user.interests << 'computers'
I'm very new to Solr and the Rails Sunspot gem, but it looks very promising for complex searching on a big database.
What I'm trying to do is allow a model in my rails app to be searched on a few fulltext columns and then a collection of its "filters" (which are just a has_and_belongs_to_many association of names).
I tried setting up my model search block as follows
self.searchable do
text :name, :boost => 5
text :description, :instructions
text :filters do
filters.map(&:name)
end
end
And my controller looks like so:
#search = ModelName.search do
keywords params[:q].to_s
end
However, I cannot seem to produce any results based on keywords found in the filters association. Am I doing something wrong? This is all very new to me.
When you initially set up your classes for search, you need to reindex the data into Solr. Have you done that? If not:
rake sunspot:reindex
Now i have something like this
http://myapp.com/pages/1
http://myapp.com/pages/2
http://myapp.com/pages/3
http://myapp.com/pages/4
And each page belong to one user
What i need is to each user to set it's own custom name for the page.
I was thinking of using the friendly_id gem http://norman.github.com/friendly_id/
but I don't find any method to directly edit the slug to set a custom friendly url
how should i proceed?
FriendlyID is a great gem.
It shouldn't be hard to implement user defined page URL.
Create table pages with user_id and link
class User < ActiveRecord::Base
has_many :pages
class Page < ActiveRecord::Base
belongs_to :user
has_friendly_id :link # link is name of the column whose value will be replaced by slugged value
On the page#new you add an input for the link attribute.
Alternatively, you could set friendly_id on title or something else with :use_slug => true option. This way FriendlyID will take the title and modify it so it doesn't have and restricted characters. It will use it's own table to store slugs. Use cached_slug to increase performanse.
Updated
To give users a choice whether they wan't to set a custom link, you could do this:
Set friendly_id on the link field without slugs..
Make a virtual attribute permalink so you could show it in your forms.
In the before_filter, check whether the permalink is set.
If it is, write it to the link field.
If it's not, write title to the link field.
FriendlyID uses babosa gem to generate slugs. If you decide to use it as well, this is how your filter could look like:
protected
def generate_link
#you might need to use .nil? instead
self.link = self.permalink.empty? ? make_slug(self.title) : make_slug(self.permalink)
end
def make_slug(value)
value.to_slug.normalize.to_s #you could as well use ph6py's way
end
Adding to_param method to one of the models should help:
def to_param
"#{id}-#{call_to_method_that_returns_custom_name.parameterize}"
end
Hope this is what you are looking for :)
I am not using the friendly_url gem and am not sure whether my way is efficient. But it works fine for me.
I have a model called Node with id and friendly url field called url_title.
My routes.rb file:
resource 'nodes/:url_title', :to => 'Nodes#view'
nodes_controller.rb
class NodesController <ActiveController
def view
#node = Node.find_by_url_title(:params(url_title))
end
end
And use the #node variable to populate your view.
Now, whenever I type www.example.com/nodes/awesome-title , it takes me to the proper page. One argument against this can be need to create an index on a non-primary field. But I think that might be required for better performance even in the friendly_url gem. Also, the non-primary field url_title needs to be unique. Again, this might be required even for correct working for friendly_url .
Feel free to correct me if I am wrong in these assumptions.
There are a variety of ways, you can achieve this-
1) using Stringex
2) sluggable-finder
3) friendly_id
A complete step by step methodology with reasons for each to be used can be found out here. Happy reading!