rails3-jquery-autocomplete foreign key - foreign-keys

Hi I need help with setting up the rails3-jquery-autocomplete gem with a manually assigned foreign key in my database.
Here is what my models look like
class User < ActiveRecord::Base
has_many :reservations, :foreign_key => 'reserver_id'
attr_accessible :login, :first_name, :last_name
end
class Reservation < ActiveRecord::Base
belongs_to :reserver, :class_name => 'User'
attr_accessible :reserver, :reserver_id, :reserver_login
end
The users table in my database has all of the columns
The Reservations Controller has
autocomplete :users, :login
Routes.rb has
resources :reservations do
get :autocomplete_users_login, :on => :collection
end
And in the reservations view I have this
<%= f.autocomplete_field :reserver_id, autocomplete_users_login_reservations_path %>
Now when I try and test it I see that the calls are being made in my javascript console but I get the error 500. For example I tried searching for xno which is in a login column of my users database.
GET http://0.0.0.0:3000/reservations/autocomplete_users_login?term=xno 500 (Internal Server Error) jquery.js:8241
jQuery.ajaxTransport.send jquery.js:8241
jQuery.extend.ajax jquery.js:7720
jQuery.each.jQuery.(anonymous function) jquery.js:7246
jQuery.extend.getJSON jquery.js:7263
a.railsAutocomplete.fn.extend.init.a.autocomplete.source autocomplete-rails.js:17
$.widget._search jquery-ui.js:6547
$.widget.search jquery-ui.js:6540
(anonymous function) jquery-ui.js:6335
Does this have anything to do with the fact that I am using foreign key in my database? If yes how should I structure my routing. I was following the gem documentation and tried to set it up so that I can look up the users table and list people by the login column of the users table, but so that the id gets returned and stored as :reserver_id once the user is selected. Previous code that worked with select field was
<%= f.select :reserver_id, User.select_options, :prompt => true %>
where select_options method creates an array of login strings and id pairs in individual arrays.
I know that the solution is probably really easy, thank you for your help...

My problem was silly but it turned out it had nothing to do with the fact that the category table used a custom foreign key. My first problem was using plural version of the table name rather than the singular. Once I changed all references to users to user I noticed that javascript console wasn't throwing any exceptions at me so I figured that I had either broken everything and the call wasn't being made or it was going through and it wasn't rendering properly.
The presence of proper MYSQL calls in the console confirmed my suspicion. That's when I saw that the list was rendering way off to the right of my page probably due to strange styling happening somewhere else.
Bottom line is having a custom foreign key doesn't change usage of the gem at all. THe original table name should be used.

Related

Rails 4/ActiveAdmin: Add link to list all objects from has_many relationship

I am using a cached_counter to keep track of all comments for a user.
My model relationship look like this:
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
comment belongs to user, counter_cache: true
end
and the user table holds the counting variable.
In Activeadmin i have a column :comments_count which shows the amount of comments for each user.
So far so good.
Now i would like to modify it a bit. I would like to add a link which directs to a page where all comments are listed. How can do this?
I am checking the AA [live demo][1] since they do something similar there.
My idea was to create a partial view and link_to it. But i m struggling implementing it and i dont know if this is best practise anyways.
I have this query
Comments.where(:User_id => :id)
but how to i embed it into a column?
Thank for any advice .
You can handle this situation by linking to the admin index page for Comment with a filter pre-set for the given user_id. This requires drastically less code then a custom page, and gives you direct access to scopes, filters, etc.
Here's how:
index do
# make sure you set sortable, so you can click to sort!
column :comments_count, sortable: 'users.comments_count' do |user|
link_to user.comments_count,
admin_comments_path(q: { user_id_eq: user.id })
end
end
Edit:
The ActiveAdmin index controller uses Ransack to handle searching and filtering. Ransack accepts query options in the form of a hash that obeys a sort-of DSL (the user_id_eq bit above is an example). Now if you open any ActiveAdmin index route and start playing around with filters, you'll see those parameters tacked on to the end of the url using the same convention. The ?q=... part gets passed directly to Ransack in the index controller, and that's how your models get filtered. Our code above simply links to the index page with the id filter pre-set. You can add other filters, sort orders, or even scopes as well.

Active Admin Associations

Active Admin has an opinionated way of showing associations that works excellent as long as the association has a field called name. I know there's a way to tell Active Admin which field to show from the associated file but I cannot find it anywhere in the documentation.
I have a model called app_label_translation that belongs_to app_label. app_label has a field called label that I'd like Active Admin treat as it would a field named name.
As a work-around I'm doing this:
index do
selectable_column
id_column
column :app_label, sortable: "app_labels.label" do |a|
link_to a.app_label.label admin_app_label_path(a.app_label)
end
end
Does anyone know the command to override name with a field of your choice?
Update: I'm using a slightly better work-around now by having alias_attribute :name, :label in my app_label model. This allows active admin to do its thing with label. I still think there's a better way to do this.
Just pass in another argument before the model you want to display in the column.
Taken from their documentation: http://activeadmin.info/docs/3-index-pages/index-as-table.html
index do
selectable_column
column "My Custom Title", :title
end
That should do the trick.

dots in rails URL

I'm trying to add a show page to my devise users but can't seem to get the id value passed properly, how do I create a link to the show page? For some reason rails is doing this to URL
/show.1
users controller:
def show
#user = User.find(params[:id])
end
user/show.html.erb
<h1><%= #user.username %></h1>
link to user profile also gives me issues id no method
<%= link_to #post.email, users_show_path(#user.id) %>
routes.rb
get "users/show"
get "pages/faq"
get "pages/about"
devise_for :users
resources :posts
You have defined the show route as:
get "users/show"
Notice that there is no dynamic segment in this route, like :id. That means your route is not expecting any value to be passed. BUT while linking to this route you passed #user.id
<%= link_to #post.email, users_show_path(#user.id) %>
which the route was not expecting. So, Rails assumed that you are passing the format (like .html, .json, etc.) which is why you see the url formed as users/show.1.
To fix this, I would suggest you to add a dynamic segment to your route to capture the id of a user correctly.
Change
get "users/show"
With
get "users/show/:id" => "users#show", as: :users_show
For a user with id = 1, when you click on the user profile link the url generated would be http://yourdomain/users/show/1

How to use activemodel to validates date_time_select

I am using rails 4.0
I wonder how to validate date_select with activemodel
let's suppose I have the code as follow
app/models/book.rb
class Book
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :title, :written_on
validates :title, presence: true, allow_blank: false
# How validates :written_on if I use date_select Please look in my view below
end
app/controllers/books.rb
class BooksController < ApplicationController
#...
def new
#book = Book.new
end
def create
#book = Book.new(book_params)
if #book.valid?
#Do something
end
end
#...
end
app/views/books/new.html.erb
<% form_for #book do |f| %>
...
<%= f.date_select :written_on %>
...
<% end %>
I have also try adding
attr_accessor 'written_on(1i)'
to my book model but I got the error invalid attribute name 'written_on(1i)'
Really appreciated for the help here.
Just to clarify, I think you're asking why you're not even able to set the written_on attribute, let alone validate it-- when I used your exact code locally and tried to create a new book, on submit I got undefined method `written_on(1i)=' for Book.
This is because the Book model isn't inheriting from ActiveRecord::Base; you're just including ActiveModel::Model and ActiveModel::Validations. The Rails guide on form helpers says "when Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type."
So I started looking through the Rails source to see where this functionality was implemented, and it's currently in ActiveRecord::AttributeAssignment. There is currently an open pull request that moves this functionality to ActiveModel so that in cases like yours, you'd be able to use it by including ActiveModel::AttributeAssignment.
I'm not sure what you can do until that gets merged in and released. I tried including ActiveRecord::AttributeAssignment and still got the same error, and looking at the pull request, it doesn't seem to be that straightforward. You could fork Rails and apply that pull request, but you'd have to maintain your own Rails for a while until that lands, then get back on a released version.

Multi select file upload field with nested attributes in Rails

Currently I have a Note model which accepts nested attributes for an Attachments model, which uses Carrierwave. When adding a Note, I have a nested form to allow attaching file to the new Note:
Nested form field:
<%= f.file_field :image, multiple: true, name: "attachment[file]" %>
I am using the Cocoon gem to add the nested field. While I can easily let them add multiple file upload fields with Cocoon, and add multiple attachments that way, I only want to load one file upload field, and let them use multi select to select multiple images.
When I do this, the file upload field says '2 Images' next to it. However, upon form submission, only one file is listed under 'attachments_attributes'. I need all of the attachments to submit at once as the Note has not yet been saved.
What is the proper way to accomplish this? I am aware of the Railscast on this topic however it did not seem to address my particular scenario.
Any help is appreciated.
Just append [] to your params
<%= f.file_field :image, multiple: true, name: "attachment[file][]" %>