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
Related
I just created a new controller and a couple actions, but when I try to look at them on localhost I get the error: No route matches {:action=>"about", :controller=>"user"}
the controller looks like this:
class UserController < ApplicationController
def index
end
def register
#title = 'Register'
end
end
shouldn't I just be able to go http://localhost:3000/user/register and have something appear after creating a new controller?
What am I doing wrong here?
UPDATE - Added Routes.rb
Rails.application.routes.draw do
get 'user/index'
get 'user/register'
get 'site/index'
get 'site/about'
get 'site/help'
root 'site#index'
end
For the action to be accessible from the browser you will need to create routes to that endpoint. Here is a guide regarding routing
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
How to load routes from database?
I have table Post with column :url. There is part of the url in this column, e.g.:
about
progs/us
progs/us/info
empty
etc.
How to set routes for this? Result example:
http://mysite.com/progs/us for the page http://mysite/posts/2
You can intercept any url by this route:
# routes.rb
get '*url' => 'posts#show', format: false
Note: this route should be declared last in your config
And controller looks like following:
# posts_controller.rb
class PostsController < ApplicationController
def show
#post = Post.find_by_url!(params[:url])
# by default "show" view is rendered with "post" variable
end
end
Here's how I made routes like this
www.example.com/jenny250
www.example.com/bob1985
At end of routes.rb
get ':username' => 'users#show'
In the users controller
def show
#user = User.find_by_username(params[:username])
# ...
end
I'm new to rails so it may sound quite naive.I'm getting this error
No route matches [GET] "/"
Here is my routes.rb
MyApp::Application.routes.draw do
match 'welcome/contact' => 'welcome#index'
end
Here is my controller
class WelcomeController < ApplicationController
def index
redirect_to :action => :contact
end
def contact
end
end
And i have a contact.html.erb in my app/view/welcome/.What am i doing wrong?
I don't understand what you want to do. But I think you want your view Welcome/contact as your index page, if this is correct, you only have to change your routes.rb file like this:
root to: 'welcome#contact'
and you have to remove the index.html file from the public folder.
On the other hand, you can read more of rails routes here
You need to create a route for the actions other than CRUD actions in controller.This will solve the issue for all actions.
match ':controller(/:action)'
What you want to do is to RENDER the contact page, not to redirect to another controller and action.
Just put the code in the contact view into the app/views/welcome/index.html.erb file, and live happily.
You need to add a contact action to your WelcomeController
class WelcomeController < ApplicationController
def index
redirect_to :action => :contact
end
def contact
end
end
When I go to my localhost:3000/users page, I get:
Unknown action
The action 'index' could not be found for UsersController.
If you were following Hartl's tutorial,then accessing localhost:3000/users will cause this error. Try localhost:3000/signup instead.
You need not define the default actions (assuming appropriate http method), all you need to do is add the following to your config/routes.rb
resources :users
First you need to make sure that your controller actually has an index action, so
class UsersController < ApplicationController has to include the def index ... end in it.
Also, make sure your routes are set up correctly using
resources :users
and check it by typing
rake routes
in the terminal to check that the routes are right.
You might also want to check that the root is set up correctly in the config/routes.rb file
if you got the same problem with me (I cant access the localhost:3000/users but I can access my localhost:3000/signup), it might be works for u.
First, in your users_controller.rb (Controller for Users) , add
def index
end
Then , make a file "index/html/erb" in your app/views/users/index.html.erb
and put this code
<% controller.redirect_to "/signup" %>
You might have to rerun your server, and it works on my problem.
Remember, if you've included
get 'signup' => 'users#new
resources :users
…in your routes.rb file, then you need to use localhost:3000/signup instead. I believe if you removed get 'signup' => 'users#new and left only resources :users then using localhost:3000/users would take you to the new user signup form.
Remove the debugger line. Also, make sure you have exit the rails console.
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
debugger
end
def new
end
end