routes.rb
resources :carts do
resources :cart_items
end
rake routes:
cart_cart_items POST /carts/:cart_id/cart_items(.:format) cart_items#create
When I click on the link, it should create new item in cart_items:
link_to 'Add to Cart', '#'
I need help with this link. Also how to pass :cart_id through the link.I'm a newbie to rails. Thanks in advance.
Assuming you have #cart, then doing link_to 'Add to Cart', cart_path(#cart) will suffice.
Related
I am stuck with a strange issue. I have googled and tried everything that was possible. All in vain and I am at the same place.
Listing the details
I have a route in routes file as resources :projects
The routes generated is as
admin_projects GET
/admin/projects(.:format) admin/projects#index
POST /admin/projects(.:format) admin/projects#create
new_admin_project GET /admin/projects/new(.:format) admin/projects#new
edit_admin_project GET /admin/projects/:id/edit(.:format) admin/projects#edit
admin_project GET /admin/projects/:id(.:format) admin/projects#show
PUT /admin/projects/:id(.:format) admin/projects#update
DELETE /admin/projects/:id(.:format) admin/projects#destroy*
My form is
form_for [:admin, #project], format: :js,remote: true, html: {id: 'edit-project-form', :method => :put } do |f|
...form fields
<td><%= f.submit 'Save', class: "btn primary save" %></td>
The URL being generated and the form tag on inspection is as follow
IF i manually update this post to put through firebug it gets updated Otherwise its throwing me a no routes matched error
(No route matches [POST] "/admin/projects/46.js"):
Please Help me figuring out whats going wrong.
remove 'format: :js' as 'remote: true' will send this as js request.
that is causing the whole issue.
I have a layout for my admin area called 'layout_admin' . In 'layout_admin', I have :
<li><%= link_to 'Contenu', :action=>'index', :controller=>'contents' %></li>
<li><%= link_to 'Petitions', :controller => 'petitions', :action => 'index' %></li>
The first link for Contenu works fine, but the second one (for petitions) drives me to a strange error :
Routing Error
No route matches {:controller=>"admin/edito"}
In the address bar I have : localhost:3000/admin/petitions
In routes.rb I have :
namespace :admin do resources :petitions
end
I must also precise that "edito" is another controller outside the admin area which has one action "index". In routes.rb I have get "edito/index" concerning edito_controller.
Does somebody have an idea of the source of the problem ?
Thansk.
Full Rake routes :
temoignages GET /temoignages(.:format) temoignages#index
POST /temoignages(.:format) temoignages#create
new_temoignage GET /temoignages/new(.:format) temoignages#new
edit_temoignage GET /temoignages/:id/edit(.:format) temoignages#edit
temoignage GET /temoignages/:id(.:format) temoignages#show
PUT /temoignages/:id(.:format) temoignages#update
DELETE /temoignages/:id(.:format) temoignages#destroy
admin_petitions GET /admin/petitions(.:format) admin/petitions#index
POST /admin/petitions(.:format) admin/petitions#create
new_admin_petition GET /admin/petitions/new(.:format) admin/petitions#new
edit_admin_petition GET /admin/petitions/:id/edit(.:format) admin/petitions#edit
admin_petition GET /admin/petitions/:id(.:format) admin/petitions#show
PUT /admin/petitions/:id(.:format) admin/petitions#update
DELETE /admin/petitions/:id(.:format) admin/petitions#destroy
admin_contents GET /admin/contents(.:format) admin/contents#index
POST /admin/contents(.:format) admin/contents#create
new_admin_content GET /admin/contents/new(.:format) admin/contents#new
edit_admin_content GET /admin/contents/:id/edit(.:format) admin/contents#edit
admin_content GET /admin/contents/:id(.:format) admin/contents#show
PUT /admin/contents/:id(.:format) admin/contents#update
DELETE /admin/contents/:id(.:format) admin/contents#destroy
admin_posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
GET /admin/posts/new(.:format) admin/posts#new
GET /admin/posts/:id/edit(.:format) admin/posts#edit
GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
admin_backend_index GET /admin/backend(.:format) admin/backend#index
POST /admin/backend(.:format) admin/backend#create
new_admin_backend GET /admin/backend/new(.:format) admin/backend#new
edit_admin_backend GET /admin/backend/:id/edit(.:format) admin/backend#edit
admin_backend GET /admin/backend/:id(.:format) admin/backend#show
PUT /admin/backend/:id(.:format) admin/backend#update
DELETE /admin/backend/:id(.:format) admin/backend#destroy
lois_index GET /lois/index(.:format) lois#index
lois_show GET /lois/show(.:format) lois#show
edito_index GET /edito/index(.:format) edito#index
reponses_index GET /reponses/index(.:format) reponses#index
reponses_show GET /reponses/show(.:format) reponses#show
lettres_index GET /lettres/index(.:format) lettres#index
lettres_show GET /lettres/show(.:format) lettres#show
accueils POST /accueils(.:format) accueils#create
new_accueils GET /accueils/new(.:format) accueils#new
edit_accueils GET /accueils/edit(.:format) accueils#edit
GET /accueils(.:format) accueils#show
PUT /accueils(.:format) accueils#update
DELETE /accueils(.:format) accueils#destroy
root / accueil#index
See the route names in the left column of your rake routes? Use that info to build calls to different path helpers:
<li><%= link_to 'Contenu', admin_contents_path %></li>
<li><%= link_to 'Petitions', admin_petitions_path %></li>
In general, that's how you should be building URLs in your Rails 3+ app.
Read through Rails Routing from the Outside In for more information (particularly 2.3: Paths and URLs).
Update:
You should look into using these _path-style helpers on the view that's being pulled up when you visit /admin/petitions.
I bet you have a link on the page similar to this:
<%= link_to "Link Text", :controller => "edito", :action => "index" %>
It's trying to find edito in the admin namespace because that's where you are within the app when you visit /admin/petitions.
To fix, you need to update it to read like this:
<%= link_to "Link Text", edito_index_path %>
Wash, rinse, and repeat for all links, forms, and url_for references in your app.
I have a back button setup on my Show Event page and its simply:
<%= button_to "Back",events_path %>
When i click this to return to my events index I get the message The action 'create' could not be found for EventsController. That's true I dont have a create action but why is it looking for one? It should just be returning me to the index of events and I'm not passing any parameters correct?
I tried adding :only => [:index, :show] to my routes entry but that didn't solve the problem. Any other suggestions or could you explain why it is trying to create? Thanks!
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
The default method is POST. You want:
<%= button_to "Back", events_path, :method => :get %>
I am updating to apply dmarkow's suggestions.
in my routes.rb:
resources :userhome do
member do
get :edit_profile_picture
end
member do
post :update_profile_picture
end
end
rake routes result:
edit_profile_picture_userhome
update_profile_picture_userhome
the link in the user home page:
<%= link_to "update profile picture", edit_profile_picture_userhome_path(#user) %>
controller:
def edit_profile_picture
#user = current_user
end
error message:
No route matches {:action=>"edit_profile_picture", :controller=>"userhome"}
I missed the fact that i didn't change the name of my view to match my controller and route. I'm going to follow naming conventions more closely to help me avoid this kind of mistake.
You need to provide a Userhome ID or object to your path. Assuming that the currently-displayed Userhome is #userhome:
<%= link_to "update profile picture", profile_picture_userhome_path(#userhome) %>
I am very new to ROR and I love it so far as I develop my first app. I have a question related to my application template as I apply formatting to the nav menu.
Is it possible to check if a url path matches the root:to path set in config.rb? I have a helper method that returns the string "current" which adds the css class style to highlight the selected menu item. The helper method works fine as long as I'm not at the homepage. When I my url is www.localhost:3000/ the css current class is not applied to the Products link since the request_uri = "/" which doesn't equal "/products". I would like the css class "current" applied to the Products menu item when I'm on the homepage.
Is there any conditional logic I can use to get the root:to path and check if it matches the is_current's parameter path?
Here's my code:
routes.rb root:to setto point to the products index view
root :to => 'products#index'
application.html.erb
<%= link_to 'Products', products_path, :class => is_current(products_path) %>
<%= link_to 'Reports', reports_path , :class => is_current(reports_path) %>
application_helper.rb
def is_current(path)
if request.request_uri == path
return 'current'
end
end
Any help would be greatly appreciated.
Thanks,
bkasen
Would this work for you?
if current_page? root_path
for more info: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page%3F
If I read correctly, you want "onlink" css applied? so that when the user is on the home page, the home icon is a different color or something. If so then apply this in your header partial or where ever:
<li><%= link_to_unless_current "Home", root_path, :class => "navLinks" do link_to "Home", root_path, :class => "navLinks", :id => "onlink" end %></li>
I know this is an old question, but it may prove useful to someone else :)