error with rails3-jquery-autocomplete - ruby-on-rails

I have installed the rails3-jquery-autocomplete gem and its shownin the list of gems.
I have generated the autocomplete-rails.js file and included it in my layout.
I am trying to implement auto complete field for the users table on the names field.
I am getting an error saying
"undefined local variable or method `autocomplete' for #<#<Class:0xb709ac24>:0xb7099a7c>"
Please let me know where I had went wrong.
releases_controller.rb
class ReleasesController < AuthorizedController
# GET /releases
# GET /releases.xml
autocomplete :users, :name
def new
#release = Release.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #release }
end
end
My view file:
= form.autocomplete_field_tag 'tester_name', :data-autocomplete=>"releases_autocomplete_users_name_path"
my routes file:
get 'releases/autocomplete_users_name'
Error:
ActionView::Template::Error (undefined local variable or method `autocomplete' for #<#<Class:0xb71cc1d8>:0xb71caa90>):
37: -puts "testers=#{#testers}"
38: = form.label :tester_tokens, "Testers"
39: = form.text_field :tester_tokens
40: = autocomplete_field_tag 'tester_name', :data-autocomplete=>"releases_autocomplete_users_name_path"
41: - #testers.each do |tester|
42: %tr
43: %td=tester.name

please update your route file this may be help you, as it is working for me:
resources :releases do
get :autocomplete_user_name, :on => :collection
end
chears..

Maybe don't use tag, as in the rails3-jquery-autocomplete page.
try
form.autocomplete_field :tester_name, autocomplete_tester_name_releases_path
I think you should also create a virtual field on releases model if you want to use :tester_name.
if you want to grab the id, then you can create a hidden field and add :id_element => '#some_element' to the autocomplete helper

Related

Form_for is giving me a No Method Error when trying to create a new object

So as it stands I have a form partial which starts off as:
<%= form_for(#merchandise) do |f| %>
It works perfectly for editing the data that I have already assigned in the rails console. When I try to use it for a "new" form that creates new merchandise (in this case the singular form of merchandise, I don't have nested resources, multiple models etc.), I get a no Method error that states
"undefined method 'merchandises_path' for #<#<Class:0x64eaef0>:0x95d2370>".
When I explicitly state the URL in the form_for method:
<%= form_for(#merchandise url:new_merchandise_path) do |f| %>
I get it to open and I finally have access to the form, however in this case I get a routing error that states
"No route matches [POST] "merchandise/new"".
I decided to write out that route in my routes file which previously just had:
root "merchandise#index" resources :merchandise
After I add the route it literally does nothing. I click submit and it takes me to the form but there is no new data in the database. I am at a complete loss and have been at this for hours googling and stack overflowing and I just don't know what to do anymore. All help is seriously appreciated. I'm adding a pastebin with all my code in the following url:
http://pastebin.com/HDJdTMDt
I have two options for you to fix it:
Option 1:
Please try to do this for best practice in Rails:
routes.rb
change your routes use plural
resources :merchandises
merchandises_controller.rb
Rename your file controller and class into MerchandisesController
class MerchandisesController < ApplicationController
def index
#merchandise = Merchandise.all
end
def new
#merchandise = Merchandise.new
end
def create
#merchandise = Merchandise.new(merchandise_params)
if #merchandise.save
redirect_to merchandises_path
else
render :new
end
end
def show
#merchandise = Merchandise.find(params[:id])
end
def edit
#merchandise = Merchandise.find(params[:id])
end
def update
#merchandise = Merchandise.find(params[:id])
if #merchandise.update(merchandise_params)
redirect_to #merchandise, notice: "The movie was updated"
else
render :edit
end
end
def merchandise_params
params.require(:merchandise).permit(:shipper, :cosignee, :country_arrival_date, :warehouse_arrival_date, :carrier, :tracking_number, :pieces, :palets, :width, :height, :length, :weight, :description, :cargo_location, :tally_number, :customs_ref_number, :released_date, :updated_by, :country_shipped_to, :country_shipped_from)
end
end
Option 2:
If you want to not change many code
/merchandise/_form.html.erb
in partial file
/merchandise/new.html.erb
<%= render 'form', url: merchandise_path, method: 'post' %>
/merchandise/edit.html.erb
<%= render 'form', url: category_path(#merchendise.id), method: 'put' %>

NoMethodError when going to page to create new record

I keep struggling with this sort of thing in Rails. I'm trying to create a statistics record for a given city. I guess I don't need to paste my models? Below is the error, URL, view and controller code.
Here's my error:
NoMethodError in Statistics#new
Showing new.html.haml where line #2 raised:
undefined method `statistics_path' for #<#<Class:0x007faf5e172928>:0x007faf5e717ef0>
The path I go to is:
http://127.0.0.1:3000/cities/1/statistics/new
Routes:
city_statistics GET /cities/:city_id/statistics(.:format) statistics#index
POST /cities/:city_id/statistics(.:format) statistics#create
new_city_statistic GET /cities/:city_id/statistics/new(.:format) statistics#new
edit_city_statistic GET /cities/:city_id/statistics/:id/edit(.:format) statistics#edit
city_statistic GET /cities/:city_id/statistics/:id(.:format) statistics#show
PUT /cities/:city_id/statistics/:id(.:format) statistics#update
DELETE /cities/:city_id/statistics/:id(.:format) statistics#destroy
cities GET /cities(.:format) cities#index
POST /cities(.:format) cities#create
new_city GET /cities/new(.:format) cities#new
edit_city GET /cities/:id/edit(.:format) cities#edit
city GET /cities/:id(.:format) cities#show
PUT /cities/:id(.:format) cities#update
DELETE /cities/:id(.:format) cities#destroy
routes.rb:
resources :cities do
resources :statistics
end
routes.rb:
Controller:
def new
#statistic = Statistic.new
respond_to do |format|
format.html # new.html.haml
format.json { render :json => #statistic }
end
end
View:
%legend New Stat
= form_for(#statistic) do |f| ###### ERROR HERE #######
= f.label :city_id
= f.text_field :city_id
.actions
= f.submit "Add", :id => "add-statistic", :class => "btn btn-primary"
EDIT added routes.rb
Two things.
As #nathan points plural form of city in English is cities so there's something wrong with your routes, maybe you renamed it? (please include routes.rb)
You have a route for statictic which is a nested resource of city, so for form helper you shoud pass
= form_for([#city, #statistic]) do |f|
Youre path file shows citys instead of cities. Perhaps you were bitten by an ActiveResource pluralize problem?

Undefined method *_path when trying to use form_for

Category controller:
def new
#cat = Category.new
respond_to do |format|
format.html
end
end
View:
%p Add new category:
~form_for(#cat) do |f|
%div.field
~f.label :name
~f.text_field :name
%div.field
~f.label :description
~f.text_area :description
%div.field
~f.submit
Routes:
resources :category
When I try to load category/new in the browser I get:
undefined method `categories_path' for #<#<Class:0x10d9c9ee8>:0x10d9b0768>
Extracted source (around line #3):
1: %h1 Category#new
2: %p Add new category:
3: ~form_for(#cat) do |f|
4: %div.field
5: ~f.label :name
Any ideas why my form isn't showing?
Also, on my category/index page, where I want to show all categories, under the list of categories I'm getting #<Category:0x10d736b40>. Can I get rid of that somehow?
The route should be
resources :categories
not
resources :category
A bit long for a comment so I've added the following as an answer instead.
If you want a singular resource you need to do:
resource :category
Which will generate only 6 routes (no index):
GET /category/new new
POST /category create
GET /category show
GET /category/edit edit
PUT /category update
DELETE /category destroy
But your controller will still be plural, unless you do the following:
resource :category, controller: :category

Redmine: Custom filter field for adding News

I'd like to add a custom filter field to "Add News" page in Redmine,
so that when I add a new news I could select group of users the email should be sent to.
The field itself is a list of Redmine User groups and every user is assigned to at least 1 of them.
Has anybody done this? Any suggestions would be appreciated
I've located the 3 files related to the issue:
/app/controller/news_controller.rb
/app/models/news.rb
/app/views/news/_form.html.erb
Environment:
Redmine version 2.2.1.stable.11156
Ruby version 1.8.7 (x86_64-linux)
Rails version 3.2.11
Environment production
Database adapter MySQL
Redmine plugins:
no plugin installed
So far I've done only 1 modification in Redmine, which sends added news to all registered users.
File: /app/modelsmailer.rb
Overview:
EDIT: Following your advice I moved mailer function to the controller:
def create
#news = News.new(:project => #project, :author => User.current)
#news.safe_attributes = params[:news]
#news.save_attachments(params[:attachments])
if #news.save
#news_added(#news)
if params[:group]
mail :to => GroupsUser.find(params[:group][:ids]).joins(:users).select("users.mail").compact,
:subject => "[#{#news.project.name}] #{l(:label_news)}: #{#news.title}"
else
render :new
end
end
end
But I'm getting error: NameError (uninitialized constant NewsController::GroupsUser): pointing to line
mail :to => GroupsUser.find
news_controller.rb:
def new
#news = News.new
#groups = GroupsUser.all
end
news/_form.html.erb:
<%= label_tag :group_ids "Groups"
<%= collection_select :group, :ids, #groups, :id, :name, {}, multiple: true %>
Edit:
I'm going to have to take a few guesses on what your controllers look like, but I'll give you something close. Based on the mailer function you provided, I'm assuming that was called out of the create controller after the News was saved. I would call the mail function after that. Something like this:
def create
news = News.new(params[:news]
if news.save
news_added(news)
send_mail_to_groups(params[:group][:ids]) if params[:group]
redirect_to ...
else
render :new
end
end
The mailing part should be removed from news_added
def news_added(news)
redmine_headers 'Project' => news.project.identifier
#author = news.author
message_id news
#news = news
#news_url = url_for(:controller => 'news', :action => 'show', :id => news)
end
in favor of its own new routine:
send_mail_to_users_by_group_ids(group_ids)
# Woo: Sent to all users, despite their email settings
mail :to => GroupsUser.find(group_ids).joins(:users).select("users.mail").compact,
:subject => "[#{#news.project.name}] #{l(:label_news)}: #{#news.title}"
end
You might want to add a where clause to only include active users.
I think that's about right. I'm doing it off the top of my head so there's probably a typo or error or two in there. Hopefully it points you in the right direction though.

How do I use belongs_to and has_many correctly?

I have been debugging this the entire day.
I have two models in my application: teaClass & tea. In teaclass.rb, I have
has_many :teas
In tea.rb, I have 'belongs_to :teaclass`.
I try to make the url looks like this "..teaclasses/:id/teas/:id"; so in teas_controller.rb, I put before_filter :get_teaClass
def show
#tea = #teaclass.teas.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #tea }
end
end
def new
if #teaclass.teas
#tea = #teaclass.teas.new
#teaclass.teas << #tea
##tea = Tea.new
else
flash[:notice=>"failed"]
#tea = Tea.new
#teaclass.teas << #tea
end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #tea }
end
end
def get_teaClass
begin
#teaclass = Teaclass.find(params[:teaclass_id])rescue
redirect_to teaclass_path, :notice => "Teaclass Required!"
end
end
But I keep getting an error saying "unknown attribute: teaclass_id"
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:inassign_attributes'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `each'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `assign_attributes'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2474:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:in `new'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2178:in `with_scope'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:in `with_scope'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:376:in `method_missing'
/home/jianpchen/repo/Teashop/app/controllers/teas_controller.rb:31:in `new'
Can anyone help me on this? Thank you.
I try to make the url looks like this "..teaclasses/:id/teas/:id"; so in teas_controller.rb, I put before_filter :get_teaClass
This only matters if you are trying to get nested routes setup but id doesn't sound like you are doing that.
What you need to do is actually add the foreign id to the database. So check schema.rb and make sure that column exists.
Here is what you need to do.
Make sure you actually have the foreign in the database table tea.
If you do skip to the next step where I will show you a better way to write your code.
If you do not have 'tea_class_id' as an integer value on your 'tea' table you need to add it.
script/generate migration add_tea_class_id_to_tea
rake db:migrate
Now This is how your code should be written
routes.rb
map.resources :teas
map.resources :teas_classes
Models
models/tea.rb
class Tea < ActiveRecord::Base
belongs_to :tea_class
end
tea_class.rb
class TeaClass < ActiveRecord::Base
has_many :teas
end
controllers
teas_controller.rb
def new
#tea = Tea.new
end
def show
#tea = tea.find(params[:id)
end
def create
#tea = Tea.new(params[:tea])
if #tea.save
redirect_to teas_path
else
render :action => 'new'
end
end
views
This is really important. Make sure you are passing the :tea_class_id as a parameter when you create a tea otherwise it doesn't know how to make the association. It's kinda behind the stage because you send params[:tea] but it is in those parameters where the tea_class_id is actually sent.
So... in your view you need to have some sort of way for users to choose a category or as you have it tea class and that is usually done with a select box when it is a one to many association.
new.html.erb
<% form_for(#tea) do |t| %>
<%= t.collection_select :tea_class_id TeaClass.all, :id, :name %>
<% end %>
Make sure you have tea classes to actually fill the collection_select method. Google that plus rails api if you don't get what's going on.
Nested Routes (on the side)
Looked like you were trying to get the routes like teaclasses/:id/teas/:id. This is called nested routing and you will want to set that up in your routes.rb
map.resources :tea_classes_ do |tea_classes|
tea_classes :teas
end
map.resources :teas
Then you can link to teas_classes/pour/teas/chinese. You should know this command rake routes. It will help you understand how the paths work.
But if you just want the link to get going it should be like this:
<%= link_to "Teas", tea_classes_teas_path(#tea_class)%>
You need to supply #teas do the link because it takes the id from that and when you click it gives it to the teas_controller' asparams[:teas_class_id]`. You don't need to do anything without. It will automatically be in the url.
If you look at your logs, you'll see what the params has contains. Given your code and explanation, there is not key teaclass_id in params. You most likely are looking for :id. If you want to access :teaclass_id, you'll probably need to set that up in your route
match 'controller/:teaclass_id' => 'controller#show'

Resources