Rails route with Spree - ruby-on-rails

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

Related

Routing Error No route matches [GET] "/about_path"

I am beginner to ror, I am trying dynamic page in my application, while I click the about link in my application I got a routing error(as said in title). Here is my routes.rb file
Rails.application.routes.draw do
devise_for :users
resources :userdetails do
collection {post :import}
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "userdetails#index"
get 'about', to:'userdetails#about'
devise_scope :users do
get 'sign_in', to: 'devise/sessions#new'
get 'sign_out', to: 'devise/sessions#destroy'
end
end
here is index.html.erb file:
<div class="topnav">
Home
News
Contact
<a href="about_path" data-method = "get" >About Us</a>
...
</div>
Please help me identify my mistake.....
your routes says: -
get '/about', to: 'userdetails#about', as: :user_about
so according to this
About Us
Instead of this you can use rails view helper which fill convert this to html anchor tag: -
<%= link_to 'About Us', user_about_path%>
As you are beginner so you should go though this rails Routing for better clarification.
Note: - by default link is GET type http verb so you don't need to mention data-method = "get"
and you can see all your routes at console by running command rake routes
When you run rails routes or rake routes it will generate like this
about GET /about(.:format) userdetails#about
That means your path is about_path then you can call out like this
About Us
or like this HTML
About Us
or like this .erb
<%= link_to 'About Us', about_path %>
While your routes like exist
get 'about', to:'userdetails#about'
Here you have defined method get that's why this is unnecessary to HTML call out data-method = "get".
Hope it will work

Ruby on Rails - Route to home#index

This one is a two-parter. Initially I had just one controller:
$ rails generate controller home index
...and I made that my root.
Recently, though, I wanted to get the database involved. So I made a scaffold called Daily and switched the root to dailies#index instead of home#index.
That worked just fine. Everything seems to be working fine.
...only problem is that I want to use home#index for something else... but now I have no way to link to it.
This is my routes.rb file:
LiquidAdmin::Application.routes.draw do
resources :dailies
devise_for :users
get '/auth/:provider/callback', to: 'sessions#create'
get "home/bandwall"
get "home/index" :to => "home#index"
# get "dailies/index"
root :to => "dailies#index"
and these are my routes:
home_index GET /home/index(.:format) home#index
I've tried.
<% link_to "Home", home_path %>
<% link_to "Home", home_index %>
<% link_to "Home", home/index %>
Nothing will get me back to that file... even though it was working just fine when it was the root.
Now all of this could've been avoided if "Plan A" had worked...
Plan A was just to make this Daily scaffold and do something like:
<% #dailies.each do |daily| %>
This worked as a link from the Daily index... but not as a link from home#index. Which is why I switched the root. I was getting annoyed and it was easier.
So my questions: 1. How do I link to the old home#index? 2. How should I have queried the dailies tables from home#index?
Assuming you have a Daily model.
routes.rb
resources :dailies
get "home/index"
Running rake routes:
dailies GET /dailies(.:format) dailies#index
POST /dailies(.:format) dailies#create
new_daily GET /dailies/new(.:format) dailies#new
edit_daily GET /dailies/:id/edit(.:format) dailies#edit
daily GET /dailies/:id(.:format) dailies#show
PUT /dailies/:id(.:format) dailies#update
DELETE /dailies/:id(.:format) dailies#destroy
home_index GET /home/index(.:format) home#index
app/controllers/dailies_controller.rb
class DailiesController
def index
#dailies = Daily.all
end
end
app/views/dailies/index.html.erb
<%= link_to "Home", home_index_path %>
<% #dailies.each do |daily| %>
<%= link_to 'A daily', daily_path(daily) %>
<% end %>

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| %>

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