DEPRECATION WARNING: ...Please use form_for(#resource, :as => :name) instead - ruby-on-rails-3

Based on other posts and a Goog search, I have tried several variations to get rid of the deprecation warning but wind up with syntax errors in every case.
<%= form_for :user, #user, :url => update_reviewer_email_userhome_path do |f| %>
warning:
DEPRECATION WARNING: Using form_for(:name, #resource) is deprecated. Please use form_for(#resource, :as => :name) instead.
I'm not sure, but I'm wondering if the fact that the model is user but the view and the update action are generated by the userhome controller.
UPDATE:
When i change to the following...
<%= form_for #user, :url => update_reviewer_email_userhome_path do |f| %>
I got this error...
No route matches "/userhome/19/update_reviewer_email"
until i changed the route from post to put:
resources :userhome, :except => [:show, :new, :edit, :update, :destroy] do
member do
put :update_reviewer_email
end
end
Thanks!

You can get rid of the :user part:
<%= form_for #user, :url => update_reviewer_email_userhome_path do |f| %>

Related

Devise scope controller not found

I created the following devise scope:
devise_scope :users do get
get 'spotkey' => 'spotkeys#spot_page'
get 'dashboard' => 'spotkeys#dashboard'
post 'dashboard' => 'spotkeys#dashboard'
get 'signup' => 'users/registrations#new', :as => :new_user_session
post 'signin'=> 'users/sessions#create', :as => :user_session
delete 'signout' => 'users/sessions#destroy'
end
controllers/users.rb
class UsersController < ApplicationController
def index
end
def show
#user = User.find_by(id: params[:id])
end
def dashboard
#keys = Spotkeys.all
#keys = Spotkeys.new
#spotkeys = Spotkeys.all
#spotkeys = Spotkeys.new
end
end
views/spotkeys/dashboard.hrml.erb
<div class="key">
<%= #keys.location %><br/>
<%= #keys.picture_url %><br/>
<%= #keys.floor_number %><br/>
<%= #keys.description %><br/>
<%= #keys.floor %><br/>
<%= #keys.buzzer_code %><br/>
<%= #keys.parking_info %><br/>
<%= #keys.cross_street %><br/>
<%= #keys.public_transit %>
</div>
I'm getting the follow error:
missing :controller key on routes definition, please check your routes
Please let me know if you need to see any other files.
Problems I noticed:
Regarding to this comment and devise how-to wiki, devise_scope needs the resource name in singular (devise_for, in contrary, needs the resource name in plural).
You also have a weird get on the same line with devise_scope- it might cause the problem.
Routing mapper, from where the error is raised, also suggests that it does not find such controller. It probably tries to search a controller based on your plural form of devise_scope. OR it tries to find controller name after your extra-get keyword after :users do.

when using semantic_form_for accurs "ActionView::Template::Error (undefined method `my_leaders_path' for #<#<Class...)"

model:
class MyLeader < ActiveRecord::Base
extend Enumerize
belongs_to :interviewer
attr_accessible :interviewer_id, :is_leader, :content
enumerize :is_leader, :in => %w[yes no]
end
controller:
ActiveAdmin.register MyLeader, :namespace => :fieldwork do
form do |f|
render :partial => 'content'
end
end
rake routes:
batch_action_fieldwork_my_leaders POST /fieldwork/my_leaders/batch_action(.:format) fieldwork/my_leaders#batch_action
fieldwork_my_leaders POST /fieldwork/my_leaders(.:format) fieldwork/my_leaders#create
new_fieldwork_my_leader GET /fieldwork/my_leaders/new(.:format) fieldwork/my_leaders#new
edit_fieldwork_my_leader GET /fieldwork/my_leaders/:id/edit(.:format) fieldwork/my_leaders#edit
fieldwork_my_leader GET /fieldwork/my_leaders/:id(.:format) fieldwork/my_leaders#show
PUT /fieldwork/my_leaders/:id(.:format) fieldwork/my_leaders#update
DELETE /fieldwork/my_leaders/:id(.:format) fieldwork/my_leaders#destroy
_content.html.erb:
<div style="width:80%;margin-left:400px">
<%= semantic_form_for MyLeader.new do |f| %>
<%= f.input :interviewer_id, :as => :hidden%>
<%= f.input :is_leader%>
<%= f.actions do %>
<%= f.action :reset, :as => :button %>
<%= f.action :submit, :as => :button %>
<% end %>
<% end %>
The ERROR is:
ActionView::Template::Error (undefined method `my_leaders_path' for #<#<Class:0x007fadc5f8cdf0>:0x007fadc3d929e8>):
Why this happened: undefined method `my_leaders_path'?
should that be fieldwork_my_leaders_path?
Seems like your form cost you an issue.
Here is example from documentation.
Partials
If you want to split a custom form into a separate partial use:
ActiveAdmin.register Post do
form partial: 'form'
end
Which looks for something like this:
app/views/admin/posts/_form.html.arb
active_admin_form_for resource do |f|
inputs :title, :body
actions
end
This is a regular Rails partial so any template engine may be used.
Make sure that you put _content.html.erb in right folder.

Rails 3 - form_for pluralization causing path/method error on "new" action but not on "edit"

When I go to the path: /genre/new in my application I get this error:
myapp/app/views/genre/_form.html.erb where line #1 raised:
undefined method `genres_path' for #<#<Class:0x007fdcb39edcb0>:0x007fdcb39e8080>
However when I go to /genre/:id/edit the _form.html.erb file renders without error and the record is updated with no problems.
My new.html.erb and edit.html.erb files call <%= render 'form' %> and my _form.html.erb file has:
<%= form_for(#genre) do |f| %>
<%= f.label :title %> <br /> <%= f.text_field :title %>
<%= f.label :desc %> <br /> <%= f.text_field :desc %>
<%= f.submit %>
<% end %>
In genre_controller.rb my 'new' and 'edit' actions are as follows:
def new
#genre = Genre.new
current_user.authorize! :create, #genre # cancan authorization
respond_to do |format|
format.html # new.html.erb
format.json { render json: #genre }
end
end
def edit
#genre = Genre.find(params[:id])
current_user.authorize! :update, #genre # cancan authorization
end
I've run a search in my codebase for the string "genres" and the only place it occurs is in the logs, so I'm sure this is not a typo in my code.
My guess is that Rails routing system correctly pluralizes "genre" to "genre", but form_for (or a dependency) is creating the pluralization "genres", but only when the parameter passed to it is empty or "new".
Given the error is around 'genres_path', I tried various combinations of the following in my routes.rb file, but they didn't solve the problem:
match "/genres" => "genre#index", :as => :genre
match "/genres/:id(.:format)" => "genre#show", :as => :genre
match "/genre" => "genre#index", :as => :genres
match "/genre/:id(.:format)" => "genre#show", :as => :genres
Any thoughts on how I can work around this?
EDIT: Here are the routes generated by the resources :genre statement in my routes.rb file:
genre_index GET /genre(.:format) {:action=>"index", :controller=>"genre"}
POST /genre(.:format) {:action=>"create", :controller=>"genre"}
new_genre GET /genre/new(.:format) {:action=>"new", :controller=>"genre"}
edit_genre GET /genre/:id/edit(.:format) {:action=>"edit", :controller=>"genre"}
genre GET /genre/:id(.:format) {:action=>"show", :controller=>"genre"}
PUT /genre/:id(.:format) {:action=>"update", :controller=>"genre"}
DELETE /genre/:id(.:format) {:action=>"destroy", :controller=>"genre"}
on new.html.erb try
<%= form_for(#genre, :url => genre_path, :method => :post) do |f| %>
assuming you have your route setup as a resource - resources :genre
also this will not work on edit.html.erb
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
Update:
this is the one we are interested in
POST /genre(.:format) {:action=>"create", :controller=>"genre"}
try this
<%= form_for(#genre, :url => {:action=>"create", :controller=>"genre"}, :method => :post) do |f| %>

Nested Routes Broken: Rails 3

I'm having a devil of a time getting this particular nested route to work. It's odd, because I've been migrating a number of routes to the new Rails 3 syntax and this one in particular just doesn't seem to work. Here goes.
I've got an object called "piece" which has a nested object called "piece_comment". Here's what the routes.rb looks like:
resources :piece do
resources :piece_rating, :as => :rating
resources :piece_comments, :as => :comments
end
And here is what piece/show.html.erb looks like, with a form to submit a piece comment:
<% #piece_comment = PieceComment.new(:piece_id => #piece.id, :user_id => current_user.id) %>
<%= form_for [#piece, #piece_comment] do |f| %>
<%= f.hidden_field 'piece_comment', 'user_id' %>
<%= f.hidden_field 'piece_comment', 'piece_id' %>
<%= f.text_area 'piece_comment', 'comment' %>
<%= f.submit_tag 'Post' %>
<% end %>
Now, what's weird is that I get the following error triggered by the "form_for" line:
undefined method `piece_piece_comments_path' for #<#<Class:0x007f80ec732a48>:0x007f80ec737ae8>
Shouldn't the :as in my routes file be sending it to piece_comments_path, and not piece_piece_comments_path? if I change it to :as => :foobar or something, I get the same error. So clearly the routes file would not seem to be working correctly. (Oddly, the behavior of the rating route seems fine.)
Any ideas for what might be wrong with the routing?
Altough I'm not sure it is the problem, resources should be plural in the routes.rb. Try with:
resources :pieces do
resources :piece_ratings, :as => :ratings
resources :piece_comments, :as => :comments
end
Use rake routes to see the name of the routes generated by the routes.rb.

In Rails 3, using Formtastic and Devise, generic routes producing errors when adding a new entry for a model

I have a very simple rails 3 program with 2 models: a user model for Devise and a writing model that captures a text field and the user's id.
My routes file is pretty basic:
devise_for :users
resources :users, :writings
root :to => "users#index"
And my form for writings, using Formtastic, is as well:
<% semantic_form_for(#writing, :html => {:method => :put}) do |f| %>
<%= f.input :main %>
<%= f.input :user_id, :collection => current_user, :as => :hidden %>
<%= f.buttons %>
<% end %>
When I try to create a new writing, the form looks great, but then when I hit submit, I get the following error:
No route matches "/writings"
I've run rake routes, and everything else seems to be working on, and I am using the default generate scaffold from rails, so the controller is the out of the box controller.
Any ideas on where I went astray?
Chris, try putting the declaration of the form like this
<% semantic_form_for #writing do |f| %>
<%= f.input :main %>
<%= f.input :user_id, :collection => current_user, :as => :hidden %>
<%= f.buttons %>
<% end %>
I've the idea that when you specify the :html parameter, you "override" some defaults in formtastic. Sorry, I'm not an expert on formtastic. I've used a bit and then decided to go for simple_form :).