No route matches [GET] "/api/v1/users" - ruby-on-rails

I am getting this error and can not figure out why. All my other routes work in local and remote when I open in browser but users. I am totally new to ruby and Xcode
thanks for any help

Looks to me like you need to also specify the :index action when defining the users routes:
ParkApp::Application.routes.draw do
namespace :api do
namespace :v1 do
resources :users, only: [:index, :create, :update] do
collection do
match '/me', to: 'users#show', via: :get
end
end
## Needed to allow cross origin request from webapp
match '/users/:id', to: 'users#update', via: :post
resources :sweetches, only: [:create, :update, :index]
match '/sweetches/:id', to: 'sweetches#update', via: :post
# Get the messages to display on the views
match '/message_views', to: 'message_views#index', as: :message_views, via: :get
resources :posts
end
end
match '/admin', to: 'admin#index', via: :get
match '/admin', to: 'admin#create', via: :post
match '/admin/:id', to: 'admin#destroy', as: :delete_fake, via: :delete
end
This should make the route you are trying to follow valid. If you take a look at the last snippet in your question you will see that the api/v1/users GET route isn't defined, my change to your routes will fix that.

You are getting the error because, as the error says, your application does not have a route that matches "/users".
See api_v1_users_path POST /api/v1/users(.:format) api/v1/users#create in your post. This means that the POST request to path /api/v1/users invokes the create method of your API::V1::UsersController. So send request to /api/v1/users, but not to '/users`.
I've never used Objective-C, but I guess you need to replace NSString stringWithFormat:#"/users" with NSString stringWithFormat:#"/api/v1/users".
Note that you need to send a POST request. Opening the above path in browser sends a GET request to your application, resulting in 404 not found error.

Related

Undefined method or local variable in rails

undefined local variable or method `requestuser_path' for #<#<Class:0x007f92de073e10>:0x007f92e191a020>
I don't know why this error occurs even though I route to the required controller and the view is also no problem here.
# routes.rb
resources :users do
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
#resources :requests, only: [:create]
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/request', to: 'users#requestuser'
The named route:
<% if signed_in? %>
<li><%= link_to "Requests", requestuser_path %></li>
<% end %>
UsersController:
def requestuser
#title = "Requests"
#user = User.find(params[:id])
#users = #user.followed_users.paginate(page: params[:page])
end
I think there is an error in the named route as I am new to Rails. I don't know what is the cause of error. The named route is predefined or how does it work.
I need to learn more topics on Rails can someone tell which is the best site to learn after beginner stage.
The requestuser_path route helper method does not exist.
Unless specified, route helper methods are autogenerated in Rails. To see a list of all these helpers and their corresponding controllers and actions, go to http://localhost:3000/rails/info/routes assuming you are running your development Rails server on port 3000.
In your case, the method you are looking for is request_path, not requestuser_path
To learn more about routes in Rails, the official documentation is a good resource. https://guides.rubyonrails.org/routing.html
Try not to use ruby keyword as routes or any controller's or model's name. Ruby will throw error. Try to rename the routes "match '/request', to: 'users#requestuser'"
It's always a good practice to run rails routes or rake routes to see how rails generates your routes, in your case pleas use request_path
It's not recommended to use match and even considered invalid in newer rails version:
If you want to expose your action to both GET and POST, add via: [:get, :post] option.
If you want to expose your action to GET, use get in the router:
Instead of: match "controller#action"
Do: get "controller#action"

Rails exception: No route matches [GET] "/clock_events/1/clock_in"

It used to work but after some changes, Action Controller is catching an exception
Routing Error: No route matches [GET] "/clock_events/1/clock_in"
routes.rb file:
Rails.application.routes.draw do
root to: 'clock_events#index'
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'
resources :clock_events, except: [:destroy] do
member do
post 'clock_in', to: 'clocks#clock_in'
post 'clock_out', to: 'clocks#clock_out'
end
end
resources :users, except: [:destroy]
end
You've defined clock_in with the post http verb, here:
resources :clock_events, except: [:destroy] do
member do
post 'clock_in', to: 'clocks#clock_in'
post 'clock_out', to: 'clocks#clock_out'
end
end
But, you're trying to use the get verb, as indicated here:
Routing Error: No route matches [GET] "/clock_events/1/clock_in"
You need to either change your path to use the get verb:
resources :clock_events, except: [:destroy] do
member do
get 'clock_in', to: 'clocks#clock_in'
post 'clock_out', to: 'clocks#clock_out'
end
end
Or modify your link (or whatever) to use the post method.
Also, your clock_in and clock_out actions are called on the clocks controller, not the clock_events controller, as indicated by your to: directive:
resources :clock_events, except: [:destroy] do
member do
post 'clock_in', to: 'clocks#clock_in'
post 'clock_out', to: 'clocks#clock_out'
end
end
Are you sure you don't want to use the ClockEventsController? If so, you could do:
resources :clock_events, except: [:destroy] do
member do
post :clock_in
post :clock_out
end
end
In which case you would get:
clock_in_clock_event POST /clock_events/:id/clock_in(.:format) clock_events#clock_in
clock_out_clock_event POST /clock_events/:id/clock_out(.:format) clock_events#clock_out
clock_events GET /clock_events(.:format) clock_events#index
POST /clock_events(.:format) clock_events#create
new_clock_event GET /clock_events/new(.:format) clock_events#new
edit_clock_event GET /clock_events/:id/edit(.:format) clock_events#edit
clock_event GET /clock_events/:id(.:format) clock_events#show
PATCH /clock_events/:id(.:format) clock_events#update
PUT /clock_events/:id(.:format) clock_events#update

Ruby on Rails Routing for Wildcard URL

I was trying to pull a segment from the URL and since I'm a rookie, kind of confused by the routing.
My code to get the last segment works fine, it's my routing that is messed up.
Ideally the URL would like this:
http://localhost.com/track/item/1234
I'm using Rails v4.0.8 & Ruby v2.0.0p451
The error is No route matches [GET] "/track/item/1234"
Here's the whole routes.rb file:
SepContact::Application.routes.draw do
get "track/item"
get "track/item/:id"
get "contacts/contact"
resources "contacts", only: [:new, :create]
end
I think CWitty's should work as well but here is a more explicit format.
match "track/items", to: 'controller#index', via: :get, as: "items"
match "track/items/:id", to: 'controller#show', via: :get, as: "item"
Note I updated your url to be more rails like items rather than item
I think most of your problem is with the track segment of the url.
I don't see how get 'track/items' would map the the items#index controller / method
I think the match method would be needed here to explicitly map the url to the correct controller and method.
Is there a good reason you are naming you url like that?
You can read all about routing here:
http://guides.rubyonrails.org/routing.html
Here is the section of the above document that discusses using the match method:
3.7 HTTP Verb Constraints
In general, you should use the get, post, put, patch and delete methods to constrain a route to a particular verb. You can use the match method with the :via option to match multiple verbs at once:
match 'photos', to: 'photos#show', via: [:get, :post]
You can match all verbs to a particular route using via: :all:
match 'photos', to: 'photos#show', via: :all
Routing both GET and POST requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.
Your routes should be like:
SepContact::Application.routes.draw do
get "track/item/:id", to: 'controller#action'
get "track/item", to: 'controller#action'
get "contacts/contact" to: 'controller#action'
resources :contacts, only: [:new, :create]
end
You need to specify a to: pointing to a controller and action unless you use the resource or resources helper.

Deploy static pages to domain root and rails application to subdomain

I'm following Michael Hartl's Rails Tutorial and deploying to Heroku.
I have static pages that are public to every web visitor and dynamic and "protected" pages that require the user to sign in in order to view them. Currently all pages are deployed to the website's root: example.com/static-page and example.com/users/1/
My objective:
deploy static pages to the root, like example.com/static-page
deploy rails' pages to a subdomain, like app.example.com/users/1
I assume the solution involves changing the routes file. Is there any tutorial or video explaining how to do so? I'm a newbie on Rails.
My routes file:
Dcid::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root 'static_pages#home'
match '/home', to: 'static_pages#home', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
You could either have a controller serving your pages or simply put your HTML files in public an treat them as assets.
In either case if they are really static, you might want to cache heavily or put a CDN in front of everything.
You'll want something like this:
#config/routes.rb
root 'static_pages#home'
#Subdomain
constraints subdomain: 'app' do
resources :users
end
#Pages
pages = %w(home about)
for page in pages do
get "/#{page}", to: "static_pages##{page}"
end
#Resources
resources :users do
get :new, as: :collection
end
resources :sessions, only: [:new, :create, :destroy] do
get :signin, action: :new, as: :collection
delete :signout, to: :destroy, as: :collection
end
This will create the routes you need. However, you won't be able to use a subdomain on Heroku, unless you use a custom domain

rails 3 routing keep custom url for post

i am fairly new to rails and want to keep the url the same for a user signing in if there is an error and the 'new' template is rendered
here are my routes
resources :users, only: [:new, :create]
resources :sessions, only: [:new, :create, :destroy]
root to: 'pages#home'
match '/signin', to: 'sessions#new'
#match '/signin', to: 'sessions#create', via: :post, as: :post_session
match '/logout', to: 'sessions#destroy'
and here is the sessions controller code
def new
end
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to root_url
else
flash.now[:error] = 'Invalid email or password'
render 'new'
end
end
as you can see,i have a custom route commented out to catch the post so that the render 'new' call keeps the /signin url, but when i do this, the flash messaging of an error does not render in the page (it does without that route though). i tried to use flash without the now method and still was not seeing my message show up. any ideas?
EDIT:
i tried the suggestions below and was still seeing the issue. after looking at the access logs, the application was routing to the first signin route because it was defined with match and not get.
my updated and working routes file now looks like this
resources :users, only: [:new, :create]
#resources :sessions, only: [:new, :create, :destroy]
root to: 'pages#home'
match '/signin', to: 'sessions#new', via: :get
match '/signin', to: 'sessions#create', via: :post, as: :post_session
match '/logout', to: 'sessions#destroy', via: :delete
Take out the now, that shouldn't be needed. Your routes are probably conflicting. Based on how you have the match lines setup, you can probably just remove the resources :sessions altogether, and then uncomment the match line. That should take care of what you need.
Also, make sure to go back to your other questions and accept some answers. A 0% acceptance rate isnt as likely to attract answers.
Edit
Based on your comments, it might just not know what to render at this time when you removed the resources call. Try changing to:
render "sessions/new"

Resources