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"}
Related
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| %>
I have two models in rails 3.1 app: lease_booking and lease_log. Lease_booking has_many lease_logs and lease_log belongs_to lease_booking. In routes.rb the association is:
resources :lease_bookings do
resources :lease_logs
end
In lease_booking show.html.erb, the path to create a new lease log is:
<%= link_to "new Log", new_lease_booking_lease_log_path(#lease_booking) %>
The 'new' code in lease_log controller is:
def new
#lease_log = LeaseBooking.find(params[:id]).lease_log.new()
end
However there is an error for lease_log controller 'new' after clicking 'new log':
Couldn't find LeaseBooking without an ID
It seems that the id (should be 1) of lease booking was not properly recognized in lease log controller.
The lease booking id (1) on the same error page is correct:
Parameters:
{"lease_booking_id"=>"1"}
Is there a way to fix it? Thanks.
UPDATE: Here is the output of rake routes
lease_booking_lease_logs GET /lease_bookings/:lease_booking_id/lease_logs(.:format) {:action=>"index", :controller=>"lease_logs"}
POST /lease_bookings/:lease_booking_id/lease_logs(.:format) {:action=>"create", :controller=>"lease_logs"}
new_lease_booking_lease_log GET /lease_bookings/:lease_booking_id/lease_logs/new(.:format) {:action=>"new", :controller=>"lease_logs"}
edit_lease_booking_lease_log GET /lease_bookings/:lease_booking_id/lease_logs/:id/edit(.:format) {:action=>"edit", :controller=>"lease_logs"}
lease_booking_lease_log GET /lease_bookings/:lease_booking_id/lease_logs/:id(.:format) {:action=>"show", :controller=>"lease_logs"}
PUT /lease_bookings/:lease_booking_id/lease_logs/:id(.:format) {:action=>"update", :controller=>"lease_logs"}
DELETE /lease_bookings/:lease_booking_id/lease_logs/:id(.:format) {:action=>"destroy", :controller=>"lease_logs"}
Your parameter output shows shat param you should be looking for - lease_booking_id :
#lease_log = LeaseBooking.find(params[:lease_booking_id]).lease_log.new()
From the console, run rake routes to see how the nested routes use the parameters.
Edit:
When you nest your routes the inner resources depend on the outer resource:
resources :lease_bookings do
resources :lease_logs
end
Look at the rake routes output for creating your lease_log:
POST /lease_bookings/:lease_booking_id/lease_logs
So when you set up your form you need to specify the lease_booking as well as the new lease_log (and any _path or _url methods will need to be adapted accordingly):
<%= simple_form_for [#lease_booking, #lease_log] do |f| %>
This would also require you to set the #lease_booking in your controller:
See http://guides.rubyonrails.org/routing.html for more information on Rails routing.
def new
#lease_booking = LeaseBooking.find(params[:lease_booking_id])
#lease_log = #lease_booking.lease_log.new
end
Here is a perfect example I found to construct both controller and view for 1-to-many association:
http://jonathanhui.com/ruby-rails-3-model-1-many-association
A dedicated admin/countries_controller is being correctly used for all actions (index, ...), except for creating records. Here the regular countries_controller from the parent controller directory is active:
Started POST "/countries" for 127.0.0.1 at 2011-06-29 23:26:38 +0200
Processing by CountriesController#create as HTML
What is missing to have the POST action being routed to admin/countries?
routes.rb:
resources :countries
namespace :admin do
resources :countries
end
rake routes:
countries GET /countries(.:format) {:action=>"index", :controller=>"countries"}
POST /countries(.:format) {:action=>"create", :controller=>"countries"}
new_country GET /countries/new(.:format) {:action=>"new", :controller=>"countries"}
admin_countries GET /admin/countries(.:format) {:action=>"index", :controller=>"admin/countries"}
POST /admin/countries(.:format) {:action=>"create", :controller=>"admin/countries"}
new_admin_country GET /admin/countries/new(.:format) {:action=>"new", :controller=>"admin/countries"}
Similar question unanswered here:
Rails help with building Admin area - Routing problem
Your form_for needs to be namespaced too:
<%= form_for [:admin, #country] do |f| %>
...
<% end %>
When you pass #country to form_for it's not going to know what namespace you want this form to go to and so it will default to just the standard POST /countries URL.
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
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"}