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!
Related
I'm trying to add routes but the link in my view does not work, I have the message
undefined local variable or method `styles_path' for #<#<Class:0x007fea4e48a3d8>:0x007fea56135c98>
my routes :
Rails.application.routes.draw do
get '/styles/:id' => 'spree/spreepages#show_taxonomy', as: 'show_taxonomy'
get '/styles' => 'spree/spreepages#choose_style', as: 'styles'
mount Spree::Core::Engine, at: '/'
root to: 'pages#home'
end
and my view
<div class="col-sm-2 choose-style-steps">
<%= link_to "", styles_path do %><div class="steps step-one">1</div><% end %>
<%= link_to('#') do %><div class="steps step-two">2</div><% end %>
<%= link_to('#') do %><div class="steps step-three">3</div><% end %>
</div>
</div>
I try with spree.styles_path but it does not work
Output of rails routes
Prefix Verb URI Pattern Controller#Action
spree / Spree::Core::Engine
root GET / pages#home
show_taxonomy GET /styles/:id(.:format) spreepages#show_taxonomy
styles GET /styles(.:format) spreepages#choose_style
Routes for Spree::Core::Engine:
locales GET /locales(.:format) spree/locale#index
set_locale POST /locale/set(.:format) spree/locale#set {:format=>:json}
skrill_cancel_order_checkout GET /orders/:order_id/checkout/skrill_cancel(.:format) spree/checkout#skrill_cancel
skrill_return_order_checkout GET /orders/:order_id/checkout/skrill_return(.:format) spree/checkout#skrill_return
new_order_checkout GET /orders/:order_id/checkout/new(.:format) spree/checkout#new
And some other Spree Route
Thank's for your help
I know it is too late but for future use.
In main_app you will get all the details about your project. So you can use it as per below,
link_to main_app.styles_url
or
link_to main_app.styles_path
I found it, just put the url in place of path like
link_to styles_url
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 am using acts_as_taggable_on steroids and I am having problem with this piece of code that generates a link to a tag:
<%= link_to tag, tag_path(:id => tag.name) %>
when I access the URL:
http://localhost:3000/tags/rails
I get the error:
No action responded to rails. Actions: show
However, this URL works:
http://localhost:3000/tags/show/rails
I have defined the show action in my tags_controller.rb
class TagsController < ApplicationController
def show
#stories = Story.find_tagged_with(params[:id])
end
end
I have the following routes generated by rake:routes :
tags GET /tags(.:format) {:controller=>"tags", :action=>"index"}
POST /tags(.:format) {:controller=>"tags", :action=>"create"}
new_tag GET /tags/new(.:format) {:controller=>"tags", :action=>"new"}
edit_tag GET /tags/:id/edit(.:format) {:controller=>"tags", :action=>"edit"}
tag GET /tags/:id(.:format) {:controller=>"tags", :action=>"show"}
PUT /tags/:id(.:format) {:controller=>"tags", :action=>"update"}
DELETE /tags/:id(.:format) {:controller=>"tags", :action=>"destroy"}
so I know that URL tags/rails points to the route tags/:id, and I've provided an additional param to link_to to assign the tag name as the :id param, but as you can see, it's not working. A forum suggested I use the to_param but I have not Tag model and the book suggested against it. Am I missing anything?
I am following the Sitepoint book Simply Rails 2
EDIT: added working URL, see top
Try adding this to your route resource:
:requirements => { :id => /.*/ }
Shooting in the dark here but should
<%= link_to tag, tag_path(:id => tag.name) %>
be
<%= link_to tag, tag_path(:id => tag.id) %>
or
<%= link_to tag, tag_path(tag) %>
Try this for your link:
link_to tag.name, { :action => :tag, :id => tag.name }
I don't know what version of rails you're using, I'm assuming 3.
Basically, you're using the tag_path which goes off of the id. If you haven't changed anything, that means something like tag/43, tag with id 43. The reason you were suggested to override to_param is in case you want it to go off the name of the tag instead, so something like tag/rails. For that, you do something like this:
class Tag
def to_param
name
end
end
Finally, you will have to change the show action to use the name, not the id. So #stories = Story.find_tagged_with(params[:name]). Then I believe that you will want to create a route to compensate for this, so above your resources :tags, add match "/tags/:name" => "tags#show".
For me it looks like the difference in routes.rb between
resources :tags
and
resource :tags
The first will have as its default index action, the second will not have :index, but it will respond with show on default route.
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"}