rails 3 routes: different auto route for model - ruby-on-rails

so i've got a model class named Photoset and a controller named Sets.
ive got resources :sets working for everything except when paths are generated off an instance of the model. for example if i use:
<%= form_for(#photoset) do |f| %>
i get the error:
no route matches {:controller=>"sets"}
ultimately i want all the uris to be .../sets/...(controller name) instead of .../photosets/...(model name)
is there any way to do this and still be able to use the helpers?
--EDIT--
heres my rake routes output:
sets GET /sets(.:format) {:controller=>"sets", :action=>"index"}
POST /sets(.:format) {:controller=>"sets", :action=>"create"}
new_set GET /sets/new(.:format) {:controller=>"sets", :action=>"new"}
edit_set GET /sets/:id/edit(.:format) {:controller=>"sets", :action=>"edit"}
set GET /sets/:id(.:format) {:controller=>"sets", :action=>"show"}
PUT /sets/:id(.:format) {:controller=>"sets", :action=>"update"}
DELETE /sets/:id(.:format) {:controller=>"sets", :action=>"destroy"}
that all works just dandy, the problem is when i try to build a form off an instance of the model. I understand that rails has no way of knowing that im trying to tie the Photoset model directly with the Set controller, but I don't know how to specify that.

You have a Photoset model, Sets controller and urls need to be in form /sets/1/edit.
resources :sets, :as => "photosets"
Works with a simple form like this:
<%= form_for(#photoset) do |f| %>
<%= f.text_field :title %>
<%= f.submit "Save" %>
<% end %>

You should set
resources :photosets, :as => "sets"
which allow you to use photosets_path, photoset_path, new_photoset_path, etc... but shows the url as sets
See here if you need more info

Related

NoMethodError when rendering a form Rails

routes.rb:
resources :shops
shop_controller.rb:
def new
#shop=Shop.new
end
new.html.erb:
<%= form_for(#shop) do |f| %>
....
<% end %>
error:
undefined method `shops_path' for:
<%= form_for(#shop) do |f| %>
The problem is that I already specify the shop resources in the routes file.
Why still get such kind of error?
Any help will be appreciated, thanks
You should use ShopsController not ShopController due to Rails naming convention.
Make sure you have these lines in your rake routes output:
shops GET /shops(.:format {:action=>"index", :controller=>"shops"}
POST /shops(.:format) {:action=>"create", :controller=>"shops"}
OR
shops POST /shops(.:format) {:action=>"create", :controller=>"shops"}
If they aren't present, look carefully at your routes.rb for possible with_options, scope or any other scoping that can affect your resources :shops in such a way that it doesn't generate default url helpers.
since u havent specified the method in the form tag, i guess it is going as a GET request. Try adding the method to ur form
<%= form_for(#shop), :method => :post do |f| %>

undefined method in rails

I get the error
undefined method `favorite_relationships_path'
when I display this form:
<%= form_for(current_user.favorite_relationships.build(lesson_id: #lesson.id),
remote: true) do |f| %>
<div><%= f.hidden_field :lesson_id %></div>
<%= f.submit "Favorite", class: "btn btn-large btn-primary" %>
<% end %>
I'm not sure why. I have a controller called favorite_relationships_controller.rb and a model file, favorite_relationship.rb, with the code
class FavoriteRelationship < ActiveRecord::Base
attr_accessible :lesson_id
belongs_to :user
belongs_to :lesson
end
My user model also has:
has_many :favorite_relationships
has_many :lessons, :through => :favorite_relationships
I'm really not sure why im getting that error. Help would be appreciated.
Rails has _path and _url helpers for routes, which are set up in config/routes.rb. You'll need to ensure that you've defined the routes for FavouriteRelationshipController; something like:
resources :favourite_relationships
You can check the routes defined for your application using the rake routes command.
You can find more information about routing at the Rails Routing from the Outside In guide.
Defining controllers, actions and views is not enough. You need to define routes in config/routes.rb to connect URLs to your controllers/actions. Defining RESTful resources with resources :favourite_relationships in your routing file is what causes Rails to generate the *_path and *_url helpers; until you do this there is no way for requests to reach your app, and no way for your app to generate routes based on your models.
Your routes file should look something like this:
MyApp::Application.routes.draw do
resources :favourite_relationships
end
This generates the typical "CRUD" routes required for a RESTful resource:
favourite_relationships GET /favourite_relationships(.:format) {:action=>"index", :controller=>"favourite_relationships"}
POST /favourite_relationships(.:format) {:action=>"create", :controller=>"favourite_relationships"}
new_favourite_relationship GET /favourite_relationships/new(.:format) {:action=>"new", :controller=>"favourite_relationships"}
edit_favourite_relationship GET /favourite_relationships/:id/edit(.:format) {:action=>"edit", :controller=>"favourite_relationships"}
favourite_relationship GET /favourite_relationships/:id(.:format) {:action=>"show", :controller=>"favourite_relationships"}
PUT /favourite_relationships/:id(.:format) {:action=>"update", :controller=>"favourite_relationships"}
DELETE /favourite_relationships/:id(.:format) {:action=>"destroy", :controller=>"favourite_relationships"}

Rails: routing and path helpers [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Rails: Routing without plurals gives strange helpers
Turns out I had :resources qtl_table in config/routes.rb twice! I get this error:
undefined local variable or method `search_qtl_table_index' for #<#<Class:0x805aff3e0>:0x805af47b0>
in app/views/qtl_table/index.html.erb:
<h2>Search for QTL</h2>
<%= form_tag search_qtl_table_index, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
and in config/routes.rb:
Qtl::Application.routes.draw do
resources :qtl_table do
collection do
get 'search'
end
end
...
end
yes I do have plurals turned off:
ActiveRecord::Base.pluralize_table_names = false
Output of rake routes:
search_qtl_table_index GET /qtl_table/search(.:format) {:action=>"search", :controller=>"qtl_table"}
qtl_table_index GET /qtl_table(.:format) {:action=>"index", :controller=>"qtl_table"}
POST /qtl_table(.:format) {:action=>"create", :controller=>"qtl_table"}
new_qtl_table GET /qtl_table/new(.:format) {:action=>"new", :controller=>"qtl_table"}
edit_qtl_table GET /qtl_table/:id/edit(.:format) {:action=>"edit", :controller=>"qtl_table"}
qtl_table GET /qtl_table/:id(.:format) {:action=>"show", :controller=>"qtl_table"}
PUT /qtl_table/:id(.:format) {:action=>"update", :controller=>"qtl_table"}
DELETE /qtl_table/:id(.:format) {:action=>"destroy", :controller=>"qtl_table"}
You may have plurals turned off, but this only affects the table names in the database, not how Rails handles routes.
Because the search route belongs to a collection, not a member, it acts on multiple model objects. Therefore, the route should be search_qtl_tables_path, note plural tables.
qtl_table is a model, and you want to search a collection of them, so it make senses that the route reads like "search multiple records".
edit: My main concern is that your rake routes shouldn't be showing those qtl_table routes twice. Are you repeating yourself somewhere in routes.rb?
OK, so you've deleted the extra routes. Now, this should work...
<%= form_tag search_qtl_table_index_path, :method => 'get' do %>
Try:
Qtl::Application.routes.draw do
resources :qtl_table do
collection do
get 'search', :as => 'search_qtl_table'
end
end
...
end

Rails: using nested named routes

routes.rb:
resources :jobs do
resources :activitylogs
end
rake routes:
...
POST /jobs/:job_id/activitylogs(.:format) {:controller=>"activitylogs", :action=>"create"}
new_job_activitylog GET /jobs/:job_id/activitylogs/new(.:format) {:controller=>"activitylogs", :action=>"new"}
edit_job_activitylog GET /jobs/:job_id/activitylogs/:id/edit(.:format) {:controller=>"activitylogs", :action=>"edit"}
...
How do I use the route new_job_activitylog?
Doing <%= new_job_activitylog %> gives undefined exception - so does using link_to which most of the examples I see are using.
Use
<%= new_job_activitylog_path %>
or
<%= new_job_activitylog_url %>
_path returns a relative path, while _url returns a complete url including http://domain.com if you've set it in your config.
To use those route names, I just had to append _path to them.
So: new_job_activitylog is undefined, but new_job_activitylog_path is a method in the view that takes the job id as a parameter.
<%= link_to 'new', new_job_activitylog_path(:job_id => #job.id) %>
works!

RoR routing very basic problem

I have Exam controller.
In routes.rb there is "resources :exams"
In controller there are REST-like generated methods.
I want to add my own method there:
def search
#exams = Exam.where("name like ?", params[:q])
end
In view file:
<%= form_tag(search_path, :method => "post") do %>
<%= label_tag(:q, "Szukaj: ") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Szukaj") %>
<% end %>
I know, there is no results presentation yet, it doesn't work at all at this moment (:
When i go to http://localhost:3000/exams/search it's mapping it to show controller and search is a :id paramether then...
How to get http://localhost:3000/exams/search to run the seach controller?
You forgot to add route. Put this in routes.rb, before resources :exams
map.search '/exams/search', :controller => :exams, :action => :search
Note, that resources :exams doesn't generate routes for all public methods of the controller, it generates very specific set of routes. You can find more information in the rails routing guide. (see section 3.2 in particular)
You'll need to add additional parameters to your mapping. You can add "collection" methods like so:
map.resources :exams, :collection => {:search => :get}
When you rake routes, you'll see that it generates something like so:
search_exams GET /exams/search(.:format) {:controller=>"exams", :action=>"search"}
exams GET /exams(.:format) {:controller=>"exams", :action=>"index"}
POST /exams(.:format) {:controller=>"exams", :action=>"create"}
new_exam GET /exams/new(.:format) {:controller=>"exams", :action=>"new"}
edit_exam GET /exams/:id/edit(.:format) {:controller=>"exams", :action=>"edit"}
exam GET /exams/:id(.:format) {:controller=>"exams", :action=>"show"}
PUT /exams/:id(.:format) {:controller=>"exams", :action=>"update"}
DELETE /exams/:id(.:format) {:controller=>"exams", :action=>"destroy"}

Resources