Customize the devise edit method - Rails 4 - ruby-on-rails

I have a problem with customising Devise method. I want to edit an existing profile. But it doesn't change.
Here's my code
My controller:
class DashboardController < Users::RegistrationsController::ApplicationController
def index
end
def update
#user = current_user
if #user.update_attributes(user_params)
redirect_to 'index', :notice => "Successfully updated profile."
else
Rails.logger.info(#user.errors.messages.inspect)
render :action => 'edit'
end
end
def edit
end
private
def user_params
params.require(:user).permit(:avatar, :username, :email, :password, :password_confirmation)
end
end
My edit view
<div class="container sign-in-up">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<br>
<div class="text-center">
<h1><b>Edit the profile</b> </h2>
<div class="tab-content">
<div class="tab-pane fade in active" id="new">
<fieldset>
<div class="form-group">
<div class="right-inner-addon">
<i class="fa fa-envelope"></i>
<%= form_for current_user, url: edit_dashboard_path(current_user), html: { method: :get, multipart: true } do |f|%>
<div class="home_field " >
<%= f.label :username%>
<br/>
<%= f.text_field :username, autofocus: true, :placeholder => 'Username', class:"form-control input-lg " %>
</div>
<div class="home_field ">
<%= f.label :email %>
<br/>
<%= f.email_field :email, autofocus: true, :placeholder => 'Email' , class:"form-control input-lg" %>
</div>
<div class="home_field ">
<%= f.label :password%>
<br/>
<%= f.password_field :password, :placeholder => 'Password', class:"form-control input-lg"%>
</div>
<div class="home_field ">
<%= f.label :password_confirmation%>
<br/>
<%= f.password_field :password_confirmation,:placeholder => 'Confirm your password' , class:"form-control input-lg"%>
</div>
<div class ="home_field">
<%= f.file_field :avatar %>
<br>
</div>
<div>
</div>
<div class="home_field">
<%= f.submit 'Edit the profile', class: "btn btn-primary btn-lg btn-block btn-group" %>
<br/>
</div>
<% end %>
</fieldset>
It can be a problem in my layout code, where i make the link to the edit page
<%= link_to "Edit the Profile", edit_dashboard_path %>

Your form is submitting to the edit dashboard using a get, which is the same page you are on. Instead you want to update the current user something like this:
<%= form_for current_user, html: { multipart: true } do |f|%>
This will default to the update attribute and CRUD action defined in your routes file for a user

I would suggest changing this line:
<%= form_for current_user, url: edit_dashboard_path(current_user), html: { method: :get, multipart: true } do |f|%>
In the url use your path to the 'update' method, not the edit (use rake routes on your command line)
In the method use :put instead of :get

Related

Modal (Devise) for Rails App

I Have an application in RoR and I need to make a login modal.
I´m using the gem devise.
sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
respond_to :js
end
In the devise folder of the views, I have the sessions folder with 2 files: _new.html.erb and new.js.erb
_new.html.erb
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">x</button>
<h2 class="dark">Log in</h2>
</div>
<div class="modal-body">
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="actions">
<%= f.submit "Log in", class: "btn btn-md btn-black-line" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
</div>
new.js.erb
$('#modaldevise').html('<%= j render('new') %>');
$('#modaldevise').modal();
In my application.html.erb I use this code to use modal:
<div id="modaldevise" class="modal fade"></div>
Finally I have a side menu that have the login link:
<%= link_to 'Login', new_user_session_path, 'data-toggle' => "modal" , 'data-target' => "#modaldevise" %>
When I press the Login link, the background becomes darker and the modal is opened, but the information from the partial _new.html.erb is not showed.
What is wrong here?
You can avoid using new.js.erb and do something like this:
application.html.erb
<div id="modaldevise" class="modal fade">
<%= render 'your_path/new' %>
</div>
side menu
<%= link_to 'Sign in', '#', "data-toggle" => "modal", "data-target"=>"#modaldevise" %>

No route matches [POST] "/contacts/new" Ruby on Rails

I'm building an interactive website through Cloud9 using a tutorial online. We are using bootstrap, JavaScript, ruby on rails, html, and scss. However, I am currently stuck. Whenever I click 'submit'...I get a Routing Error page. None of the information is stored in my db.
routes.rb
Rails.application.routes.draw do
root to: 'pages#home'
get 'about', to: 'pages#about'
resources :contacts
end
contacts_controller.rb
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to new_contact_path, notice: "Message sent."
else
redirect_to new_contact_path, notice: "Error occured."
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
contacts/new.html.erb
<div class="container">
<div class="row">
<h3 class="text-center">Contact Us</h3>
<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<div class="well">
<%= form_for "contact" do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
</div>
I followed the instructions exactly, and I have no idea what is wrong or what to change. Can someone help before I rip my hair out?
You need to change
<%= form_for "contact" do |f| %>
to
<%= form_for #contact do |f| %>
Full code
<div class="container">
<div class="row">
<h3 class="text-center">Contact Us</h3>
<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<div class="well">
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
</div>

AJAX form submitting in HTML rails 4

I've looked through several of these kind of questions but none of them work for me.
Weirdly enough the same strategy to a different form works perfectly.
I have model validations in place but when I submit an empty form I get an error of missing template of ceeate where format is HTML.
I guess the purpose of each file is self-explanatory.
My ContactsController.rb:
class ContactsController < ApplicationController
respond_to :html, :js
def new
#contact = Contact.new
end
def create
#contact = Contact.new(params[:contact])
#contact.request = request
end
end
new.js.ebr:
$("#main-block").html("<%= escape_javascript(render 'contact_form') %>")
_contact_form.html.erb:
<div align="center">
<h3>Sazinieties ar mums</h3>
<ul class="errors"></ul>
<%= form_for #contact, :html => {:class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :name, class: "control-label" %>
<div class="controls">
<%= f.text_field :name %>
</div>
</div>
<div class="control-group"><div class="field">
<%= f.label :e_mail, class: "control-label" %>
<div class="controls">
<%= f.email_field :e_mail %>
</div>
</div>
<div class="control-group"><div class="field">
<%= f.label :message, class: "control-label" %>
<div class="controls">
<%= f.text_area :message %>
</div>
</div>
<div>
</br>
<%= f.submit('Sūtīt', remote: true, class: "btn btn-primary") %>
</div>
<% end %>
</div>
create.js. erb:
<%= render 'save' %>
_save.js.erb:
$("ul.errors").html("")
<% if #contact.errors.any? %>
<% #contact.errors.full_messages.each do |message| %>
$("ul.errors").append($("<li />").html("<%= message.html_safe %>"))
<% end %>
<% else %>
$("#main-block").empty()
$("#main-block").html("Ziņa ir nosūtīta. Tuvākajā laikā sniegsim atbildi!")
<% end %>
You need to add this to your form:
<%= form_for #contact, :remote => true, :html => {:class => 'form-horizontal' } do |f| %>
And remove remote: true from your submit button.

Devise: change password reset_password_token is empty

I am using Devise & I am trying to create a new form where user's can reset their passwords, but I am getting the following error:
ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_reset_password_token"
The reason this is happening is because the reset_password_token generated from
<%= f.hidden_field :reset_password_token %>
is empty. Ex:
<input id="user_reset_password_token" name="user[reset_password_token]" type="hidden">
Here is the controller action
def update_password
#user = current_user
# raise params.inspect
if #user.update_with_password(params[:user])
sign_in(#user, :bypass => true)
redirect_to '/account_settings', :notice => "Your Password has been updated!"
else
redirect_to '/account_settings', :notice => "Your Password was not changed. Try again"
end
end
Here is my form
edit_password.html.erb
<ul class="nav nav-pills">
<li class="active">Change Password</li>
<li>Profile</li>
<li>Messages</li>
</ul>
<hr/>
<%= render :template => 'users/edit_password_form',
:locals => { :resource => current_user, :resource_name => "user" } %>
edit_password_form.html.erb
<%= form_for(resource, :as => resource_name, :url => "password/users/update_password", :html => { :method => :put }) do |f| %>
<%# devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<br>
<div class="row">
<div class="col-sm-3 col-md-3 ">
<%= f.label :current_password, "Current Password:" %>
</div>
<div class="col-sm-3 col-md-3 ">
<%= f.password_field :current_password, :placeholder =>"Current Password" %>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-3 col-md-3 ">
<%= f.label :password, "New Password:" %>
</div>
<div class="col-sm-3 col-md-3 "><%= f.password_field :password, :placeholder=>"New Password" %>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-3 col-md-3 ">
<%= f.label :password_confirmation, "Confirm New Password:" %>
</div>
<div class="col-sm-3 col-md-3 ">
<%= f.password_field :password_confirmation, :placeholder=>"Confirm New Password" %>
</div>
</div>
<br>
<br>
<%= f.submit "Change my password", :class => "btn btn-default" %>
<% end %>
I assume this has nothing to do with the built-in devise functionality where they can be sent an email to reset their password when they've forgotten it. Instead, it looks like you are allowing them to enter their current password and then choose a new one. In that case, I don't think you want the reset_password_token in the form at all, as it's irrelevant, i.e. you can just remove the line:
<%= f.hidden_field :reset_password_token %>

Edit form wrongly being submitted as GET - Rails

I'm trying to edit using a form <%= form_for #agent, :action => 'agents/update', :method => :put do |f| %> but when I click on Save Changes the form is submitted as a GET request instead of a PUT to the update action.
I have resources :agents in my routes.rb file.
In agents_controller.rb the show and update actions are:
def show
#agent = Agent.find(params[:id])
#subscription = #agent.subscription
end
def edit
#agent = Agent.find(params[:id])
#subscription = #agent.subscription
end
def update
#agent = Agent.find(params[:id])
#subscription = #agent.subscription
#agent.update_attributes(:agent)
end
It's pretty depressing when you can't get a standard edit form working, any help appreciated.
EDIT
agents/edit.html.erb
<%= form_for #agent do |f| %>
<ul>
<% for message in #agent.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
<div class="control-group">
<label class="control-label" style="width:200px;margin-right:20px"><strong>First name</strong></label>
<div class="controls">
<%= f.text_field :first_name, :placeholder => 'First name', :class => '', :style => '' %>
</div>
</div>
<div class="control-group">
<label class="control-label" style="width:200px;margin-right:20px"><strong>Last name</strong></label>
<div class="controls">
<%= f.text_field :last_name, :placeholder => 'Last name', :class => '', :style => '' %>
</div>
</div>
<div class="control-group">
<label class="control-label" style="width:200px;margin-right:20px"><strong>Email</strong></label>
<div class="controls">
<%= f.text_field :email, :placeholder => 'Your email', :class => '', :style => '' %>
</div>
</div>
<div class="control-group">
<label class="control-label" style="width:200px;margin-right:20px"><strong>Phone</strong></label>
<div class="controls">
<%= f.text_field :phone, :placeholder => 'Your phone number', :class => '', :style => '' %>
</div>
</div>
<div class="form-actions">
<%= f.submit "Save Changes", :class => 'btn btn-success' %>
<%= link_to 'Cancel', leads_path, :class => 'btn' %>
</div>
<% end %>
You shouldn't need to supply a method nor an action for a simple form like this. A simple
<% form_for #agent do |f| %>
should do you. Have you tried that? Are there any other forms on the page that could be conflicting? If so, try removing those other forms first, then see how that goes.
Fixed it. This post covered the exact same issue: HTML <form> tag causing Rails form to submit a GET instead of POST request
The GET request is being caused by a form tag around the rails form which was there to add the twitter bootstrap styling class "form-horizontal". Removed the extra form tag and it works fine.

Resources