Nested Routes Broken: Rails 3 - ruby-on-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.

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.

Rails Routing issues

I have a website in people can browse artists and their albums. I have setup my routes like so:
match 'albums/[:id]/[:album_id]' => 'albums#show', :as => 'artist_album'
I tried setting up a nested route like:
resources :artists do
resources :albums
end
but I can't figure out how to achieve the routing like in the first example... but thats a different question... This is my code when trying to render artist_album_path
<%= link_to image_tag("#{album["Images"]["Album150x150"]}", width: "122", alt: "#{#term}", class: "float-left"), artist_album_path("/#{CGI::escape(album["Artist"]["Name"])}/#{CGI::escape(album["Title"])}") %>
I keep getting this error:
No route matches {:controller=>"albums", :action=>"show", :id=>"/Beastie+Boys/Licensed+To+Ill"}
Any idea on what I am doing wrong?
In routes.rb:
match 'albums/:id/:album_id' => 'albums#show', :as => 'artist_album'
In your view:
<%= link_to image_tag(...), artist_album_path(:id => ..., :album_id => ...) %>

DEPRECATION WARNING: ...Please use form_for(#resource, :as => :name) instead

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| %>

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 :).

Routing error while using partial form in rails 3

does anyone know how to use a form partial to create and update data about an object? My update method seems to work, but I can't create a new object. Every time I clink on the 'Add new ad' I get this error:[ActionController:Routing Error in Classified#new No route matches {:controller=>classified}]. Here is the code for the partial form: The error points to the first line:
<%= form_for(#classified) do |f| %>
<p>
<%= f.label :title %><br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :price %><br/>
<%= f.text_field :price %>
</p>
<p>
<%= f.label :location %><br/>
<%= f.text_field :location %>
</p>
<%= f.label :description %><br/>
<%= f.text_area :description %>
<%= f.label :email %><br/>
<%= f.text_field :email %>
<%= f.submit %>
<% end %>
<%= link_to 'Back', {:action => 'list'} %>
Here are my methods for new, edit and update in the Classified controller class:
def new
#classified=Classified.new
end
Here is the 'def create' method:
def create
#classified = Classified.new(params[:classified])
if #classified.save
redirect_to :action => 'list'
else
render :action => 'new'
end
end
I suspect the problem is in my config/routes.rb. I already have this line:
resources :classified
I have also put:
root :to => "Classified#list"
root :to => "Classified#new"
root :to => "Classified#show"
root :to => "Classified#edit"
root :to => "Classified#form
Could the problem be with the routes.rb file. And how comes it works with the update method and not the new method? Please help. I have tried all possible tricks to no avail. I will be so glad. Thanks in advance
I would like to know the result of your rake routes, because you use the singular for your controller classified.
Could it be classifieds, with the S in all your routes? I'm not that sure because resources: classified I guess came from the scaffold and so it should be good, then you miss a `"' on your last line but this could be a typo.
I would recommend having a plural name for the resource, as that is the norm for most resources:
resources :classifieds
You will also need to change your controller name and class name to classifieds_controller and ClassifiedsController
Also, your named routes should be lowercase (although you should get rid of these routes altogether):
root :to => "classifieds#list"
Get rid of the root routes. The resources line will create all the routes you need. And you are only supposed to have one root route in routes.rb and that should point to your home page controller#action.
If you have an action for form, then you don't need that either.. just mentioning this because you created a route for "Classified#form". Controller actions are not necessary for partials.
Your new and create methods and the form look ok at first glance. Try reworking your routes first and if you are still having problems, run rake routes from the command line and post the output in your question and leave me a comment on this answer and I will see if I can help you figure it out.
Read this first:
http://guides.rubyonrails.org/routing.html