Devise authentication - custom user edit - ruby-on-rails

routes.rb:
devise_for :users, :controllers => {
:registrations => 'users',
:sessions => 'sessions',
:confirmations => 'confirmations',
:passwords => 'passwords'
}
The view for "edit" action:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= resource.inspect %>
resource.inspect shows all user's fields as nil.
Controller:
def edit
resource = params[:id].nil? ? current_user : User.find_by_id(params[:id])
end
What to put instead of "resource =" to make this thing work? I want that the view remains with default "resource" and "resource_name" variables.
IMPORTANT: I would prefer to retain the default behaviour for edit method without overriding it and without writing own code for it.

Related

How to routes to a specific controller with a variable value ahead of it in rails 5?

Normally its like:
resources :users
To keep a value ahead we can do like
scope :url do
resources :users
end
but, issue is that I have the just variable in front of the routes not a fixed value with few conditions for it.
Example in rails 2. and want to convert the routing accoding to rails5
map.with_options :controller => 'users' do |user|
user.forgot_user ':url/users/forgot', :action => 'forgot', :url => /([a-zA-Z0-9\-]*)/
user.user ':url/users/retrieve', :action => 'retrieve', :url => /([a-zA-Z0-9\-]*)/
user.login ':url/users/login', :action => 'login', :url => /([a-zA-Z0-9\-]*)/
user.logout ':url/users/logout', :action => 'logout', :url => /([a-zA-Z0-9\-]*)/
user.new_user ':url/users/new', :action => 'new', :conditions => { :method => :get }, :url => /([a-zA-Z0-9\-]*)/
user.users ':url/users/:id', :action => 'show', :conditions => { :method => :get }, :url => /([a-zA-Z0-9\-]*)/
end
Thanks in advance.
scope "/(:subdomain)", :defaults => {:subdomain => "default"} do
...
end
use subdomain as a variable.

How to use another page(not default) to edit user (Devise gem)?

How to use another page(not defaule) to edit user (Devise gem) ?
I have this situation: I need some page to add attributes to user, but I want to use edit_user_registration - I want to use another page.
So I need to create another action in registrations controller and add it in routes.rb. Also I created another file in registrations folder, but I have troubles with implementation.
Here is my registrations controller:
def paypal
end
and paypal.html.erb:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |d| %>
<%= d.email_field :paypal_email %>
...
<%end%>
Question: what I should write in my routes rb to call paypal action and edit page worked properly?
devise_for :users, :controllers => {:registrations => 'registrations', :sessions => 'sessions'} do
registrations#paypal//something here instead
end

Rails devise - how to present the login form if user is not logged in

I am new to RoR...just warning you :)
I made this route in routes.rb:
scope :module => :mobile, :as => :mobile do
constraints(:subdomain => /m/) do
resources :home
devise_for :users, :path => "", :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "signup" }, :controllers => {:sessions => "sidebar/sessions"}
resources :disclosures # Will have new, get, look up a disclosure
devise_for :users, :path => "", :path_names =>
{ :sign_in => "login", :sign_out => "logout", :sign_up => "signup" }
# How do I tell is to also take care of the confirm account screen?
end
end
And I made this HAML page:
-content_for :primary_content do
.content_container
%strong{:class => "code", :id => "message"} Hello Alex!
-content_for :primary_content do
-if signed_in?
=render "sidebar/common/primary_navigation"
-else
=render "devise/sessions/form"
and when the user is not logged in, it gives me this error:
undefined local variable or method `resource' for #<#<Class:0x147fe1990>:0x147fdab40>
and
1: = form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => {:id => "login-form"}) do |f|
2: %fieldset.field-container
3: = f.label(t :label_login)
4: .input
and this stack trace:
app/views/devise/sessions/_form.html.haml:1:in `_app_views_devise_sessions__form_html_haml__2025707780_2630401320'
app/views/mobile/home/index.html.haml:26:in `_app_views_mobile_home_index_html_haml__2141491110_2759485880'
app/views/mobile/home/index.html.haml:22:in `_app_views_mobile_home_index_html_haml__2141491110_2759485880'
app/views/mobile/home/index.html.haml:1:in `_app_views_mobile_home_index_html_haml__2141491110_2759485880'
app/controllers/mobile/home_controller.rb:6:in `index'
So if I understand it right, it doesn't know what to do with the form part of the code. Is that correct? What did I do wrong to cause this problem? How do I fix it?
Thank you!!
You don't need to render the devise form.
So remove this:
-else
=render "devise/sessions/form"
You need to add this to the top of the controllers you want to authenticate:
before_filter :authenticate_user!, :only => [:edit, :update, :destroy]
This will cause devise to redirect to the login form if the user is not authenticated.
If he is already authenticated, your normal views will be rendered.

rails3 routes.rb

I have been working with rails3, here the view.html.erb form have one login button, so when i click on that button, gives no routes matches :controller => 'home', :action => 'login'. But i have put that in routes.rb. Why this happening?
view.html.erb
<%= form_tag( { :controller => 'home', :action => 'login' }, { :method
=> 'post'}) do %>
<%= text_field(:name, :name, :class => "span2",:placeholder =>
"Username") %>
<%= password_field_tag(:password, :password, :class =>"span2") %>
<%= submit_tag "Login", :class => "btn btn-primary" %>
<% end %>
**routes.rb**
resources :home
resources :home do
post :login, :on => :member
end
**homecontroller.rb**
class HomeController < ApplicationController
def login
end
end
You have defined "resources :home" twice, first declaration is useless and overrides second.
Since you used resources to define your routes (which is recomended) you should use the helper method generated, in this case its login_home_path instead of the old syntax { :controller => 'home', :action => 'login' }
First,
You declare resources :home two times.
try this way in your routes.rb
resources :home
match '/login', to: 'home#login'
and use login_path in submit tag.
I would prefere for login, logout, create Sessions controller
rails generate controller Sessions --no-test-framework
and for login create new method and for logout(signout) create destroy method

Rails Routing Error

In routes.rb I have:
get "survey/show" => "survey#show"
post "survey/step_2" => "survey#step_2"
post "survey/step_3" => "survey#step_3"
And in step_2.html.erb I have:
<%= form_for #result, :url => { :controller => 'survey', :action => 'step_3' } do |f| %>
And in survey_controller.rb I have:
def step_2
#result = Result.new(params[:result])
if #result.save
session[:result_id] = #result.id
render :action => "step_2"
else
render :action => "show"
end
end
def step_3
#result = Result.find(session[:result_id])
if #result.update_attributes(params[:result])
render :action => "step_3"
else
render :action => "step_2"
end
end
And when I submit the form on step_2 I get the following error:
No route matches "/survey/step_3"
I believe Rails form_for method may be making that a PUT request, since the #result object has an id. I believe you should change your form_for line to:
<%= form_for #result,
:url => { :controller => 'survey', :action => 'step_3' },
:html => { :method => :post} do |f| %>
or change the route type to put in routes.rb
You have to use match.
match 'survey/step_3' => 'survey#step_3', :via => 'post'
I might be wrong about the :via, but it's something like that.

Resources