Rails partial render and devise action - ruby-on-rails

In my rails app i go for example to devise sign up action (http://*:3000/users/sign_up) and get error:
No route matches {:controller=>"devise/vehicle_types", :action=>"search_vehicle_type"}
I thing it's becouse i'm in devise namespace of routing... But how can i normally render sign up view? My layout part look's so:
= render :partial => 'vehicle_types/findtype'
and this partial:
.form
= form_tag :controller => 'vehicle_types', :action => 'search_vehicle_type' do
= select("post", "MFA_ID", Manufacturer.all.collect {|p| [ p.MFA_BRAND, p.MFA_ID ] }, {:prompt => 'Марка'}, :class => "login-input man-select")
= select_tag "models", options_for_select(['Модель', nil]), :class => "login-input mod-select", :prompt => 'Модель', :disabled => :true
= select_tag "fueltype", options_for_select([['Тип топлива', nil], ['Не важно', nil], ['Бензин', 53302], ['Дизель', 53205], ['Газ', 53241], ['Гибрид', 55554], ['Электродвигатель', 52433]]), :class => "login-input fuel-select"
= text_field_tag "year", nil, :placeholder => "Год выпуска", :class => "login-input"
.submit-area
.left
= submit_tag "Выбор", :class => "orange-button"
On other pages (non-devise) all is ok, but when i go to password recovery, or sign up i get erros. But why? And how to solve it?
Also part of routes:
Oleg::Application.routes.draw do
devise_for :users
match '/search_vehicle_type' => 'vehicle_types#search_vehicle_type'
root :to => 'pages#index'
end
Also i read about links here https://github.com/plataformatec/devise/issues/471, but how for partial to do?
and rake routes | grep devise
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy
admin_password POST /admins/password(.:format) devise/passwords#create
new_admin_password GET /admins/password/new(.:format) devise/passwords#new
edit_admin_password GET /admins/password/edit(.:format) devise/passwords#edit
PUT /admins/password(.:format) devise/passwords#update
cancel_admin_registration GET /admins/cancel(.:format) devise/registrations#cancel
admin_registration POST /admins(.:format) devise/registrations#create
new_admin_registration GET /admins/sign_up(.:format) devise/registrations#new
edit_admin_registration GET /admins/edit(.:format) devise/registrations#edit
PUT /admins(.:format) devise/registrations#update
DELETE /admins(.:format) devise/registrations#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy

While it may not be the best style, the easiest fix to this is to use an actual path in the form_tag:
.form
= form_tag "/search_vehicle_type" do
# Rest of the form
Another alternative would be to name the route in your routes.rb file:
match '/search_vehicle_type' => 'vehicle_types#search_vehicle_type', :as => :search_vehicle_type
Then use that named route in the form_tag:
.form
= form_tag search_vehicle_type_path do
# Rest of the form

Related

Rails routes error with namespace?

So I have created two routes using a 'Member' namespace, when I do rake routes, it does not display the path that I should use it is just showing this;-
GET /user/:id(.:format) member/member#
GET /user/:id/edit(.:format) member/member#edit
when I use this line it returns an error;-
<li><%= link_to image_tag(current_user.picture, class: "user-picture"), {:controller => "Member/Member", :action => :show} if current_user.picture? %></li>
and get this error;-
No route matches {:action=>"show", :controller=>"Member/Member"}
This is my routes;-
scope module: 'member' do
get '/user/:id', to: 'member#show'
get '/user/:id/edit', to: 'member#edit'
end
My whole rake routes;-
Prefix Verb URI Pattern Controller#Action
charges GET /charges(.:format) charges#index
POST /charges(.:format) charges#create
new_charge GET /charges/new(.:format) charges#new
edit_charge GET /charges/:id/edit(.:format) charges#edit
charge GET /charges/:id(.:format) charges#show
PATCH /charges/:id(.:format) charges#update
PUT /charges/:id(.:format) charges#update
DELETE /charges/:id(.:format) charges#destroy
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy
admin_unlock POST /admins/unlock(.:format) devise/unlocks#create
new_admin_unlock GET /admins/unlock/new(.:format) devise/unlocks#new
GET /admins/unlock(.:format) devise/unlocks#show
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
user_registration POST /users(.:format) users/registrations#create
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
root GET / public/public#homepage
cart_add_item POST /cart_add_item(.:format) cart/cart#add_item_to_cart
empty_cart GET /empty_cart(.:format) cart/cart#empty_cart
destroy_cart GET /destroy_cart(.:format) cart/cart#destroy
cart GET /cart(.:format) cart/cart#show
product_new GET /product/new(.:format) admin/product#new
product_create POST /product/create(.:format) admin/product#create
product_destroy GET /product/destroy(.:format) admin/product#destroy
GET /user/:id(.:format) member/member#show
GET /user/:id/edit(.:format) member/member#edit
How would I access my show action and my edit action?
I suggest you to use the Rails built-in resources in your routes:
scope module: 'member' do
resources :users, only: [:show, :edit]
end
And then you will be able to call the following paths:
member_user_path(current_user) # /member/users/:id/ -> Show action
edit_member_user_path(current_user) # /member/users/:id/edit -> Edit action
Instead of using the old-fashioned link definition:
{:controller => "member/member", :action => :show}
No route matches {:action=>"show", :controller=>"Member/Member"}
The problem is not with the routes, but with the link itself. You need to change it to below
<li><%= link_to image_tag(current_user.picture, class: "user-picture"), {:controller => "member/member", :action => :show} if current_user.picture? %></li>
Notice the change :controller => "Member/Member" to :controller => "member/member"
I am doing something like below for versioning in rails 4:
scope '/v1' do
resources :foobar, module: 'v1'
end
scope '/v2' do
resources :foobar, module: 'v2'
end
routes:
/v1/foobar/:id
/v2/foobar/:id
I have the controllers in the app/v1 and app/v2 directories

undefined local variable or method with form_tag

I'm trying to create a simple search form in my Rails application. I get an error with the url path of the form:
<%= form_tag(med_search, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
<%= button_to "Search", class: "btn btn-default" %>
<% end %>
The first line above causes error undefined local variable or methodmed_search' for #<#:0x007fab2b5afa90>`
The problem is most likely with my routes setup. I created a new controller action called search so I edited my routes.db to look like this:
resources :meds do
collection do
get 'search' => 'meds#search'
end
end
devise_for :users
#get 'meds/index'
root to: "meds#index"
resources :meds, :path => ''
end
When I do rake routes, I am seeing the path med search so I know the url is valid:
Prefix Verb URI Pattern Controller#Action
med_search GET /meds/:med_id/search(.:format) meds#search
meds GET /meds(.:format) meds#index
POST /meds(.:format) meds#create
new_med GET /meds/new(.:format) meds#new
edit_med GET /meds/:id/edit(.:format) meds#edit
med GET /meds/:id(.:format) meds#show
PATCH /meds/:id(.:format) meds#update
PUT /meds/:id(.:format) meds#update
DELETE /meds/:id(.:format) meds#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / meds#index
GET / meds#index
POST / meds#create
GET /new(.:format) meds#new
GET /:id/edit(.:format) meds#edit
GET /:id(.:format) meds#show
PATCH /:id(.:format) meds#update
PUT /:id(.:format) meds#update
DELETE /:id(.:format) meds#destroy
What should I change in the routes to fix this?
Thanks!
EDIT: changed url to med_search_path, get new error: No route matches {:action=>"search", :controller=>"meds"} missing required keys: [:med_id]. Looks like it's related to the route /meds/:med_id/search(.:format)
1) you need to change your form_tag like this
<%= form_tag(search_meds_path, :method => "get", id: "search-form") do %>
2) You need to change your route from member to collection like this
resources :meds do
collection do
get 'search' => 'meds#search'
end
end
3) Not sure why you need to add resources :meds, :path => '' at the bottom again. Incase you dont need it, it is better to remove.
It looks like you're trying to code up a search but because you put it in the resources block Rails is assuming you're talking about a specific med.
Remove the route from the resources block and change it to get 'meds/search' => 'meds#search'. That will allow you to use it as just a regular endpoint without Rails complaining that you need an ID.

Devise Update method does not have ID in route

I have two models using Devise 3. My edit and update routes do not have the /:id that I would expect and it is causing me to receive a bad PUT requestion as you can see below.
Started PUT "/users.user" for 127.0.0.1 at 2014-06-11 11:11:46 -0400
Processing by RegistrationsController#update as
Completed 406 Not Acceptable in 4894.6ms (ActiveRecord: 2.4ms)
Here are the routes
new_user_session GET /users/sign_in(.:format) sessions#new
user_session POST /users/sign_in(.:format) sessions#create
destroy_user_session DELETE /users/sign_out(.:format) sessions#destroy
user_password POST /users/password(.:format) passwords#create
new_user_password GET /users/password/new(.:format) passwords#new
edit_user_password GET /users/password/edit(.:format) passwords#edit
PUT /users/password(.:format) passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
This is what my routes.rb looks like
devise_for :users, :controllers => { :confirmations => "confirmations", registrations: "registrations", session: "sessions", passwords: "passwords" }
devise_scope :user do
authenticated :user do
root :to => 'appointments#index', as: :user_authenticated_root
get "/appointments", to: "appointments#index", as: :appointments
end
unauthenticated :user do
root :to => 'devise/registrations#new', as: :unauthenticated_root
end
match '/join' => 'devise/registrations#new'
end
My form is
<%= simple_form_for(resource, :as => resource_name, :url => user_registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
What am I missing to fix the routes to say /users/:id(.:format), etc ?
I think this should do it:
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>

Devise logout link doesn't work

Why click on link = link_to 'Logout', destroy_user_session_path, method: :delete make GET request to /users/sign_out and render show view of UsersController? My routes.rb is:
devise_for :users
resources :users, only: [:show] do
member do
get 'profile'
get 'purchases'
get 'mailing'
end
end
and /config/initializers/devise.rb contain config.sign_out_via = :delete, rake routes output is:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
profile_user GET /users/:id/profile(.:format) users#profile
purchases_user GET /users/:id/purchases(.:format) users#purchases
mailing_user GET /users/:id/mailing(.:format) users#mailing
user GET /users/:id(.:format) users#show
The html generated for your logout link is as follows:
<a rel="nofollow" href="/users/sign_out" data-method="delete"></a>
This will only work if the js files are properly loaded ( I think it needs jquery_ujs )
Otherwise it defaults to a GET request.
It should be work for you.
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>

Unable to over-write devise routes

I am using devise and I want to customize its urls:
users/sign_in --> account/login
users/sign_up --> account/register
users/edit --> account/profile
...
Now my routes.rb looks like this:
devise_scope :user do
get '/account/login' => 'devise/sessions#new'
post 'account/login' => 'devise/sessions#create', as: :user_session
delete 'account/logout' => 'devise/sessions#destroy', as: :destroy_user_session
post 'account/password' => 'devise/passwords#update', as: :user_password
get 'account/password/forgot' => 'devise/passwords#new', as: :new_user_password
get 'account/password/edit' => 'devise/passwords#edit', as: :edit_user_password
put 'account/password' => 'devise/passwords#update'
get 'account/cancel' => 'devise/registrations#cancel', as: :cancel_user_registration
post 'account' => 'devise/registrations#create', as: :user_registration
get 'account/register' => 'devise/registrations#new', as: :new_user_registration
get 'account' => 'devise/registrations#edit', as: :edit_user_registration
put 'account' => 'devise/registrations#edit'
delete 'account' => 'devise/registrations#destroy'
end
devise_for :users
I manage to over-write profile, forgot, register bug not login:
So when I click on the link "Sign up" link in the generic Devise Login form I am redirected to /register in the browser, but when I click the "Sign in" link I am still redirected to /users/sign_in rather than login.
I tried match vs. post + get for the routes but without luck. I know that if I generate the forms myself I can decide how the links look like, but I would prefer to user the generic devise forms and also I am curious why the other routes work.
Running rake routes produces this (with the top five rows from my routes):
new_user_session GET /account/login(.:format) devise/sessions#new
user_session POST /account/login(.:format) devise/sessions#create
destroy_user_session DELETE /account/logout(.:format) devise/sessions#destroy
user_password POST /account/password(.:format) devise/passwords#update
new_user_password GET /account/password/forgot(.:format) devise/passwords#new
edit_user_password GET /account/password/edit(.:format) devise/passwords#edit
account_password PUT /account/password(.:format) devise/passwords#update
cancel_user_registration GET /account/cancel(.:format) devise/registrations#cancel
user_registration POST /account(.:format) devise/registrations#create
new_user_registration GET /account/register(.:format) devise/registrations#new
edit_user_registration GET /account(.:format) devise/registrations#edit
account PUT /account(.:format) devise/registrations#edit
DELETE /account(.:format) devise/registrations#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
POST /users/password(.:format) devise/passwords#create
GET /users/password/new(.:format) devise/passwords#new
GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
GET /users/cancel(.:format) devise/registrations#cancel
POST /users(.:format) devise/registrations#create
GET /users/sign_up(.:format) devise/registrations#new
GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root / posts#show
Update: I am now mimicking all of devise's routes (See updated routes.rb). But I am still redirected to /users/sign_in
You can try this, This is working
see more help devise wiki
devise_for :users, :path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout'}
This should work. Also remove the /account and add path account to your routes file like I've done below.
devise_scope :user, path: "account" do
get "login", :to => "devise/sessions#new", :as => "login"
get "signup" => "users/registrations#new", :as => "register"
get "edit" => "edit_user_registration", :as => "edit"
end

Resources