ruby on rails - Changing Route from "/:id/model" to "/model" -
i want alter path root/carts/56 root/cart.
i applied logic question rails route username instead of id, changes id of user username in path. works because username can set unique.
i want /cart lead (obviously) 1 cart visiter (no users) working , rid of :cart/:id. do not utilize user model, otherwise @user.cart (:user/cart)
so (understandably so) error
invalid route name, in use: 'cart'
because line
match '/:carts/:id' => 'carts#show', :as =>'cart', :via => :get
relevant routes:
carts /carts(.:format) carts#index post /carts(.:format) carts#create new_cart /carts/new(.:format) carts#new edit_cart /carts/:id/edit(.:format) carts#edit cart /carts/:id(.:format) carts#show patch /carts/:id(.:format) carts#update set /carts/:id(.:format) carts#update delete /carts/:id(.:format) carts#destroy
update:
with resource :cart, :only => :show # note 'resource' not 'resources'
improvement, can remove id path somehow?
http://localhost:3000/cart?id=52
is gives me.
sorry - line gives me id in url is
<li><%= link_to "cart", cart_path(id: session[:cart_id]) %></li>
is singular resource need? creating carts in session this
<% if session[:cart_id] == nil %> <li><%= link_to "cart", new_cart_path %></li> <% else %>
the thought there beingness if haven't created cart yet, create 1 when visit page.
i followed tutorial carts here
in routes file use
resource :cart, :only => :show # note 'resource' not 'resources'
this give cart_path can call. nail show method in carts_controller. if need /root/ theres few ways add together that.
put above user path that's declared in routes.rb file.
you may need validation ensure users don't select names reserved.
ruby-on-rails
No comments:
Post a Comment