Change the name of a method in routes - ruby-on-rails

I have a controller called pages with the following methods:
class PagesController < ApplicationController
def about
end
def privacy
end
def terms
end
def contact
end
These are all static pages. I have created views with the same names. I would like to change the name of the 'contact' method to 'contact-us-form'. How can I achieve this? I have tried renaming the method to 'contact_us_form', but my view will not accept the same naming convention; it only works if I name it as 'contact-us-form'.
My routes.rb file:
%w[about privacy terms contact].each do |page|
get page, controller: "pages", action: page
end
I'm using Rails 4. How do I only change the 'contact' url in routes.rb? Thanks.

Remove contact from the array you currently have, and place this in your routes.rb:
match "/contact_us_form", to: "pages#contact_us_form", via: "get", as: "contact_us_form"
You can alternatively use:
get "/contact_us_form", to: "pages#contact_us_form"
If you want the path to use dashes instead of underscores, replace "/contact_us_form" with "/contact-us-form".
For more information, check out the Routing section in the Rails guide: http://guides.rubyonrails.org/routing.html

Related

Create views in resource in Rails

I am a beginner working with Rails: I have this routes.rb:
Rails.application.routes.draw do
resources :requirements
root "department#index"
get "department/about"
end
How can I create a view that has a path like requirements/major?
Thank you so much!
You can extend resources and add custom actions, like this:
resources :requirements do
collection do
get :major
end
end
You'll need an action in the RequirementsController that matches, e.g.
class RequirementsController < ApplicationController
def major
# set up whatever resource 'major' corresponds to
end
...
end
That's at least one way of doing it. You could also have a controller that directly supports the nested 'major' resource, which would be similar to above - just with a controller: 'name of controller' directive inline..
It'd probably pay to get your head around the "Rails Routing from the Outside In" guide: https://guides.rubyonrails.org/routing.html

static pages in rails app missing template

I have to add a static page to my rails app.
Here's the logic I am following.
I made a directory called pages and put a file called signup.html.erb there.
The path in my another html file is pages/signup
In my routes.rb, I have get 'pages/signup'
I made a pages controller and there I have something like below
class PagesController < ApplicationController
def signup
end
end
I get below error.
Missing template pages/signup, application/signup with
What's wrong here?
just write in routs.rb
get '/pages/signup', to: 'pages#signup'
and in pages_controller.rb write
def signup
#page = Page.new
end
def create
# write create function
end
In my routes.rb, try
get 'pages/signup'
match "signup", :to => "pages#signup"
on rails 4
get 'pages/signup'
match "signup", :to => "pages#signup", via: :all
Because your controller isn't in the spree namespace change the full path of the html file from
app/views/spree/pages/signup.html.erb
to
app/views/pages/signup.html.erb

Rails 4 match all routes for one controller

I have a controller with a number of static pages and I would ideally like to route them all with a wildcard.
Is it possible to do something like the following?
get 'static/:action'
Why don't you just use the show action:
#config/routes.rb
resources :static, param: :page, only: :show #-> url.com/static/:page
#app/controllers/static_controller.rb
class StaticController < ApplicationController
def show
render "#{params[:page]}"
end
end
This way, you can pass the "page" directly through the link and have it all handled by Rails:
<%= link_to "About", static_path("page") %>
You probably need something like get 'static/:action', to: 'static#show' and then in your StaticController show action render the correct static page based on the params[:action] parameter.
See http://guides.rubyonrails.org/routing.html#defining-defaults for more.
You can route something like
get '*path', to: 'static#show'

Referring to a nested resourse

I have a nested resource:
resources :res1 do
resources :res2
end
And I have a custom action in res2:
def my_action
end
which doesn't appear in the list of the pre-generated paths (there is no res1_res2_my_action_url url). I want to refer to my_action using controller and action notation but the following doesn't work:
url_for(controller: [:res1, :res2], action: :my_action)
Why is that?
The resources directive in your routes file will only create default routes for your controller.
#index
#new
#create
#show
#edit
#update
#destroy
If you want to add custom routes, you'll have to declare them like so:
resources :res1 do
resources :res2 do
get :my_action
end
end
you can hard code a specific route that points to action and controller:
get '/pathname', to: 'controller_name#my_action'
Try running rake routes and see what o/p you get,a try to apply in your view
get 'my_action' => "res2#my_action"
and then write
:url => my_action_path

Why isn't my 'join' action working, it says the action 'show' can't be found

In my UserController I have:
def join
end
I have a join.html.erb in my /views/user/ folder.
My routes has a :
resources :user
When I go to:
http://localhost:3000/user/join
I get:
The action 'show' could not be found for UserController
Re: why isn't the join action found?
To answer your specific question, what's happening is that you want to have an action "join" for your User model.
Your problem is that you haven't defined a route matching the url http://localhost:3000/user/join
The line resources :user in your routes file only defines routes for the seven standard rest verbs/actions:
index, new, create, show, edit, update, destroy
See: http://apidock.com/rails/ActionController/Resources/resources
Added: to fix, you'll need to add an explicit or generic route. Routing docs
Added: Re: why am I seeing the error message re show? To be ultra-precise, the route selector "GET /usr/:id" (created by your resource call) is being used to select the SHOW action for the User resource. The :id value is being set to "join". Since you don't have a Show method defined in your controller, that's the error that you're seeing.
You're using resources, but have a non-REST action, so you need to add the join action to the route with the appropriate HTTP verb:
map.resources :users, :member => { :join => :get }
Place:
def show
end
in your UserController.
To be certain:
app/controllers/users_controller.rb
def join
end
app/views/users/join.html.erb
config/routes.rb
resources :users

Resources