Rails 4 - partial rendered with update.js.erb not showing updated values - ruby-on-rails

On the account settings page for my Rails app I am trying to use Ajax to have the displayed users settings info updated after the update form is submitted. Right now when I submit the form the database gets updated and the _account_settings.html.erb partial appears to get re-rendered, but the partial doesn't show the updated information. For example, say the users name is "John Doe" and he clicks the '#edit_name' link. This renders the _name_form.html.erb which he can use to change his name. When he changes his name and submits the form, let's say he changes it to "Joe Blow", the form disappears and the 'Name: <%= current_user.name %> - click to edit' shows up again but the name is still "John Doe" even though the name in the DB is now "Joe Blow". If I refresh the page the name now appears as "Joe Blow". I am using devise and made a custom registrations controller to allow the user to update certain attributes without entering a password (name still requires the current password). I used this code provided by the devise and simply added
respond_to do |format|
format.html { redirect_to edit_user_registration_path }
format.js
end
to the end of the 'if #user.update_attributes(account_update_params)' block in the above link. Any ideas as to why the re-rendered partial isn't showing the updated user information?
edit.html.erb
<h1>Account Settings</h1>
<div id="account_settings_container">
<%= render 'account_settings' %>
</div>
_account_settings.html.erb
<div class="user-attribute-container">
<%= link_to "#", class: "edit-attribute", id: "edit_name", remote: true do %>
<p>Name: <%= current_user.name %> - click to edit</p>
<% end %>
</div>
<div class="user-attribute-container">
<%= link_to "#", class: "edit-attribute", id: "edit_email", remote: true do %>
<p>Email: <%= current_user.email %> - click to edit</p>
<% end %>
</div>
<div class="user-attribute-container">
<%= link_to "#", class: "edit-attribute", id: "edit_password", remote: true do %>
<p>Password: click to edit your password</p>
<% end %>
</div>
<%= javascript_tag do %>
$("#edit_name").click(function() {
$(this).hide().after("<%= escape_javascript(render('name_form')) %>");
});
$("#edit_email").click(function() {
$(this).hide().after("<%= escape_javascript(render('email_form')) %>");
});
$("#edit_password").click(function() {
$(this).hide().after("<%= escape_javascript(render('password_form')) %>");
});
<% end %>
_name_form.html.erb (the email and password form partials have :email, :password & :password_confirmation instead of :name, otherwise they're the same)
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :method => :put, :html => { id: "edit_user_name" }, remote: true) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<%= f.label :password_field, "Password" %>
<%= f.password_field :current_password %>
<%= f.submit "Update" %>
<% end %>
update.js.erb
$("#account_settings_container").html("<%= escape_javascript(render('account_settings')) %>");
From the log file:
Started PUT "/users" for 127.0.0.1 at 2014-06-09 15:01:21 -0700<br>
Processing by RegistrationsController#update as JS<br>
Parameters: {"utf8"=>"✓", "user"=>{"name"=>"Joe Blow", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}<br>
[1m[36mUser Load (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 14 ORDER BY "users"."id" ASC LIMIT 1[0m<br>
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 14]]<br>
[1m[36m (0.0ms)[0m [1mBEGIN[0m<br>
[1m[35mSQL (0.0ms)[0m UPDATE "users" SET "name" = $1, "updated_at" = $2 WHERE "users"."id" = 14 [["name", "Joe Blow"], ["updated_at", Mon, 09 Jun 2014 15:01:21 PDT -07:00]]<br>
[1m[36m (0.0ms)[0m [1mCOMMIT[0m<br>
Rendered registrations/_name_form.html.erb (15.6ms)<br>
Rendered registrations/_email_form.html.erb (0.0ms)<br>
Rendered registrations/_password_form.html.erb (15.6ms)<br>
Rendered registrations/_account_settings.html.erb (46.9ms)<br>
Rendered registrations/update.js.erb (46.9ms)<br>
Completed 200 OK in 219ms (Views: 78.1ms | ActiveRecord: 0.0ms)
Custom devise registrations controller:
class RegistrationsController < Devise::RegistrationsController
def update
#user = User.find(current_user.id)
successfully_updated = if needs_password?(#user, params)
#user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
else
# remove the virtual current_password attribute
# update_without_password doesn't know how to ignore it
params[:user].delete(:current_password)
#user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
end
if successfully_updated
# sign_in #user, :bypass => true
respond_to do |format|
format.html { redirect_to edit_user_registration_path }
format.js
end
else
respond_to do |format|
format.html { render "edit" }
format.js
end
end
end
private
def needs_password?(user, params)
user.email != params[:user][:email] || params[:user][:password].present? || user.name != params[:user][:name]
end
end

current_user is generated by Devise using a before_filter. So the object returned, will be the user as it was before that action method was called. You need to use the #user object which is the user object that is modified by the action method.
So for example:
<p>Name: <%= #user.name %> - click to edit</p>

Related

Rails commits change from form, but nothing written to DB

I think there must be something simple that I'm not setting up correctly, but I cannot figure out this behavior. The user submits and edit form, rails log shows the change is committed, but when I load the record again, it's still the same.
Here's the form
<%= form_for(#deal) do |f| %>
<div class="deal-<%= #deal.id %>">
<div class="field">
<%= f.label :headline %><br />
<%= f.text_field :headline, required: true %>
</div>
<div class="field">
<%= f.label :matter %>
<%= f.text_field :matter %>
</div>
<div class="field">
<%= f.label :summary %>
<%= f.text_area :summary %>
</div>
<%= f.submit "Update" %>
</div>
<% end %>
and relevant parts of controller
def edit
#deal = Deal.find(params[:id])
end
def update
#deal = Deal.find(params[:id])
if #deal.save
flash[:success] = 'Your deal was updated'
redirect_to root_path
else
render 'edit'
end
end
And the output on submit
Started PATCH "/deals/1" for ::1 at 2017-03-21 13:15:17 +0100
Processing by DealsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Rctg6qcVMfOutyXjZihiR5+zhagr+cduNfA6TSYqOHmjvh+dcQl9TZ/MxZbY+IMyt0LU2PQrsyCjcUJczGcTKg==", "deal"=>{"headline"=>"testheadline", "matter"=>"matter", "summary"=>"First test summary"}, "commit"=>"Update", "id"=>"1"}
Deal Load (0.1ms) SELECT "deals".* FROM "deals" WHERE "deals"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.0ms) begin transaction
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 6], ["LIMIT", 1]]
(0.1ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 5ms (ActiveRecord: 0.4ms)
def update
#deal = Deal.find(params[:id])
if #deal.update(deal_params)
flash[:success] = 'Your deal was updated'
redirect_to root_path
else
render 'edit'
end
end
private
def deal_params
params.require(:deal).permit(:headline, :matter, :summary)
end

Form does not submit data

I have a form that has a few fields that should populate to my database which it doesn't. I am unsure why this is. I defined the parameters which fixed another error but my data is not being stored. My table name is users.
My controller:
Class LandlordPageController < ApplicationController
before_action :get_user
def get_user
#user = current_User
end
def index
end
def show
end
def create
#user = User.new(user_params)
#user.save
redirect_to profile_page_index_path
end
private
def user_params
params.require(:user).permit(:address, :cityResiding, :ssn, :birthDate, :gender, :phoneNumber)
end
end
My form:
<%= form_for :user do |f| %>
<div class="field">
<%=f.label :address, 'Current Address' %><br/>
<%= f.text_field :address, :required => 'required' %>
</div>
<div class="field">
<%=f.label :cityResiding, 'Current City' %><br/>
<%= f.text_field :cityResiding, :required => 'required' %>
</div>
<div class="field">
<%=f.label :phoneNumber, 'Phone Number'%><br/>
<%= f.telephone_field :phoneNumber, maxlength: 15, :required => 'required' %>
</div>
<div class="field">
<%=f.label :gender, 'Gender'%><br/>
<%= f.select :gender, ['',' Male', 'Female', 'Other','Prefer Not to Answer']%>
</div>
<div class="field">
<%=f.label :birthDate, 'Birth Date'%><br/>
<%= f.date_select :birthDate, order: [:month, :day, :year], :required => 'required'%>
</div>
<div class="field">
<%=f.label :ssn, 'Social Security Number' %><br/>
<%= f.text_field :ssn, maxlength: 9 %>
</div>
<div class="actions">
<%= f.submit "Submit Information" %>
</div>
<% end %>
log:
Started GET "/landlord_page" for 127.0.0.1 at 2016-11-06 17:59:58 -0500
Processing by LandlordPageController#index as HTML
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering landlord_page/index.html.erb within layouts/application
Rendered landlord_page/index.html.erb within layouts/application (4.0ms)
Rendered layouts/_navbar.html.erb (1.0ms)
Completed 200 OK in 98ms (Views: 88.4ms | ActiveRecord: 0.0ms)
Started POST "/landlord_page" for 127.0.0.1 at 2016-11-06 18:00:06 -0500
Processing by LandlordPageController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"98tUidqprFyTG8ZC/tV9TRDIVlV0I+ocnQfKTUDqorlS+JMFHtaCWz69EwBvH5MrHhnRbg93m695//Z1I5xt3A==", "user"=>{"address"=>"1", "cityResiding"=>"1", "phoneNumber"=>"1", "gender"=>" Male", "birthDate(2i)"=>"11", "birthDate(3i)"=>"6", "birthDate(1i)"=>"2016", "ssn"=>""}, "commit"=>"Submit Information"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.0ms) begin transaction
(0.0ms) rollback transaction
Redirected to http://localhost:3000/profile_page
Completed 302 Found in 4ms (ActiveRecord: 0.0ms)
EDIT: My form does submit data but this error is being thrown on save
NoMethodError in LandlordPageController#create
undefined method `save' for #<Array:0x0000000d883a08>
Instead of new I went with update and removed the save, because the new already executed saving the parameters and save was trying to save nil parameters. I was not creating a new user but meaning to add new parameters to an existing user. This is why the error occured. The full final code is below.
def create
respond_to do |format|
if User.update(user_params)
format.html { redirect_to profile_page_index_path, notice: 'Landlord application successfully submitted.' }
format.json { render :show, status: :created, location: #user }
else
format.html { redirect_to profile_page_index_path, notice: 'Landlord application was not successfully submitted.' }
format.json { render :show, status: :unprocessable_entity }
end
end
end
As has been said, the issue is probably validation errors.
If you mean to update the user, you will need to use the update method instead and pass the user to the form
form_for #user
That will send a PATCH request to the update method on the controller once the form is submitted.
def update
#user.update user_params
if ! #user.valid?
flash[:error] = #user.errors.full_messages
end
redirect_to profile_page_index_path
end

Rails Paperclip inserting instead of updating

I created a simple application that has a Product and an Image model. Product has_many Images and Images has an attached file attribute (paperclip).
I created a simple_form for creating/editing Products and it works fine on creation. However, when editing a Product that has N images, rails inserts more N files - empty files.
I have set up a Simple Form custom input that tests if the image attachment exists in which case instead of rendering the builders input, it only renders an image_tag().
I see the html generated and it show something strange, a hidden tag:
<input id="product_images_attributes_0_id" name="product[images_attributes][0][id]" type="hidden" value="14">
And in the rails server console I see:
Processing by ProductsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"asdfasdfaasdf=", "product"=>{"reference"=>"Y1112CYL.E2", "images_attributes"=>{"0"=>{"id"=>"14"}}}, "commit"=>"Update Product", "id"=>"20"}
Product Load (0.6ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1 [["id", "20"]]
Unpermitted parameters: id
(0.2ms) BEGIN
SQL (1.0ms) INSERT INTO "images" ("created_at", "imageable_id", "imageable_type", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Tue, 18 Feb 2014 05:05:13 UTC +00:00], ["imageable_id", 20], ["imageable_type", "Product"], ["updated_at", Tue, 18 Feb 2014 05:05:13 UTC +00:00]]
(0.6ms) COMMIT
Here is the code to my implementation. If someone could help I would be very happy! Please pardon me if I left out any import part of the code, I will gladly edit the question to include it.
_form.html.erb
<%= simple_form_for(#product) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.input :reference %>
<h3>Images</h3>
<div id='images'>
<%= f.simple_fields_for :images do |image| %>
<%= render 'image_fields', :f => image %>
<% end %>
<div class='links'>
<%= link_to_add_association 'New image', f, :images %>
</div>
</div>
</div>
<div class="actions">
<%= f.button :submit %>
</div>
<%end%>
_image_fields.html.erb
<%= content_tag :div, class: "nested-fields images-fields" do %>
<%= content_tag :div, id: "new-image" do %>
<% if f.object.photo.exists? %>
<% f.template.image_tag(f.object.photo.url(:thumb)) %>
<% else %>
<% f.input :photo, :as => :photo %>
<% end %>
<% end %>
<% end %>
app/inputs/photo_input.erb
class PhotoInput < SimpleForm::Inputs::FileInput
def input
out = '' # the output string we're going to build
# check if there's an uploaded file (eg: edit mode or form not saved)
if object.send("#{attribute_name}?")
# append preview image to output
# <%= image_tag #user.avatar.url(:thumb), :class => 'thumbnail', id: 'avatar' %>
out << template.image_tag(object.send(attribute_name).url(:thumb), :class => 'thumbnail', id: 'photo')
else
# append file input. it will work accordingly with your simple_form wrappers
(out << #builder.file_field(attribute_name, input_html_options)).html_safe
end
end
end
ProductsController#update
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
For posterity, #omarvelous' comment seems to have solved it:
I added the reject if all blank to my accepts_nested_attributes_for
accepts_nested_attributes_for :images, :allow_destroy => true, :reject_if => :all_blank
Edit:
It turns out there is an issue with this solution. When reject_if => :all_blank is it stops the destroy from working properly. See this post that shows a workaround -- that I did not manage to get to work.

Create.js.erb Not Showing In AJAX with Rails

I am trying to get AJAX working on a form in my rails application and am about 75% of the way there. I have a form where a person can create an activity and tag that activity with skills. I would like to provide the ability for them to create a new skill right within the form. I can replace the "Create a New Skill" link with a form and then save the record without any problem. My issue is after the record is saved, rails keeps redirecting me back to the my home page rather than back to the page with the form. The redirect is normal behavior if the user was creating a skill through the typical HTML form.
The link on my page to create a new skill that turns into a form:
<%= link_to "Create A New Skill", new_skill_path, :id => "new-skill-link", remote: true %>
The new skill form that replaces the link:
<%= form_for Skill.new, :remote => true do |f| %>
<%= f.text_field :description %>
<%= f.submit "Save", :class => 'btn-large btn-primary' %>
<% end %>
My skills controller (the New and Create methods):
def new
#skill = Skill.new
#tags = current_user.tags
respond_to do |format|
format.html
format.js
end
end
def create
#skill = current_user.skills.new(params[:skill])
params[:skill][:tag_ids] ||= []
respond_to do |format|
if #skill.save
flash[:success] = "Skill was successfully created!"
format.html { redirect_to home_page_url }
format.js { render action: "create" }
else
flash.now[:error] = "There was an error saving your skill."
format.html { render action: "new" }
format.js { render action: "new" }
end
end
end
My new.js.erb:
$("#new-skill-link").hide().after('<%= j render("skills/remote_form") %>');
My create.js.erb:
$("#new-skill").remove();
$("#new-skill-link").show();
$(".skill-list ul").append('<%= j render(#skill) %>');
It seems that rails is interpreting the request as HTML so it is responding with the HTML page. I tried adding :format => :js to the skill form but rails just rendered the javascript in my browser without interpreting it.
When I look at the log on the web server, I don't see any errors. I get:
Started POST "/skills" for 127.0.0.1 at 2012-12-31 20:02:16 -0700
Processing by SkillsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxx", "skill"=>{"description"=>"Example Skill"}, "commit"=>"Save"}
User Load (62.8ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'xxxxx' LIMIT 1
(0.1ms) begin transaction
SQL (15.4ms) INSERT INTO "skills" ("created_at", "description", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["created_at", Tue, 01 Jan 2013 03:02:16 UTC +00:00], ["description", "Example Skill"], ["updated_at", Tue, 01 Jan 2013 03:02:16 UTC +00:00], ["user_id", 1]]
(1.4ms) commit transaction
Redirected to http://localhost:3000/home_page
Completed 302 Found in 96ms (ActiveRecord: 79.7ms)
Any thoughts?
The reason is, you should not do redirects inside the create method, because the AJAX request is kind of lost in the middle (you could see this via Firebug/FF).
Following is a sample code:
Ex: assuming I have a scaffold called project
projects_controller.rb
class ProjectsController < ApplicationController
#project = Project.new(params[:project])
if #project.save
flash.now[:success] = ["#{#project.name} sucessfully created"]
else
flash.now[:errors] = #project.errors.full_messages
end
end
in view/projects
_form.html.erb
<div id="message"></div>
<%= form_for #project :remote => true, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.text_field :name, :class => 'span3', :placeholder => "Project name" %>
<%= f.submit nil, :class => 'btn btn-success' %>
<% end %>
create.js.erb
$("#message").html("<%= escape_javascript raw(flash_display) %>");
in helper
module ApplicationHelper
def flash_display
response = "<div class='alert #{alert_class}'>"
response += "<button type='button' class='close' data-dismiss='alert'>x</button>"
flash.each do |name, msg|
msg.each do |m|
response = response + content_tag(:p, m)
end
end
response += "</div>"
flash.discard
response
end
private
def alert_class
css_class = case flash.first[0]
when :errors then "alert-error"
when :success then "alert-success"
when :notifications then "alert-block"
end
css_class
end
end
So as you can see, I'm not redirecting from my create method, after saving I'm just passing the message of the status. So your page will not refresh and only update the messages.

Ruby on rails redirect_to won't redirect after save, just stays on page (but does save)

Here is my code:
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to :success }
else
format.html { render :new }
format.js { render :form_errors }
end
end
end
end
View:
<div id="joinFormContainer">
<%= form_for #user, :remote => true do |f| %>
<div id="firstNameField">
<%= f.text_field :first_name, :placeholder => "First Name" %>
</div>
<div id="lastNameField">
<%= f.text_field :last_name, :placeholder => "Last Name" %>
</div>
<div id="emailFieldJoin">
<%= f.text_field :email, :placeholder => "Email" %>
</div>
<div id="passwordFieldJoin">
<%= f.password_field :password, :placeholder => "Password" %>
</div>
<div id="usernameField">
<%= f.text_field :username, :placeholder => "Username" %>
</div>
<div id="joinButton"> <%= f.submit 'Join Us', :id =>"join_submit" %> </div>
</div><% end %>
<div id="error_explanation">
<%= #user.errors.full_messages.first if #user.errors.any? %>
</div>
</div>
<p> </p>
</div>
</div>
log:
Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `password_salt`
SQL (0.9ms) INSERT INTO "users" ("created_at", "email", "encrypted_password", "first_name", "last_name", "password_salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Wed, 26 Oct 2011 23:40:03 UTC +00:00], ["email", "fdfdfdfdfdffdf#sdsd.sdd"], ["encrypted_password", "$2a$10$vFOoxHfvc3N2jNPMgx3iN.cNrxENvO5qAaSTaUa5itmzb0uADV9ZS"], ["first_name", "fddffdf"], ["last_name", "dffdffddff"], ["password_salt", "$2a$10$vFOoxHfvc3N2jNPMgx3iN."], ["updated_at", Wed, 26 Oct 2011 23:40:03 UTC +00:00], ["username", "fdgdgddgfdfd"]]
Redirected to http://localhost:3000/success
Completed 302 Found in 87ms
Started GET "/success" for 127.0.0.1 at 2011-10-27 00:40:03 +0100
Processing by UsersController#success as JS
Rendered users/success.html.erb within layouts/application (0.0ms)
Completed 200 OK in 10ms (Views: 10.1ms | ActiveRecord: 0.0ms)
After save the user isn't redirected to temporary success page. Am I missing something?
I also tried redirect_to 'success'
This simple change to the controller solved my issue:
I replaced:
format.html { redirect_to :success }
with:
format.js { render :js => "window.location = '#{success_path}'" }
all is working fine now.
Decided to not bother with recaptcha for now
EDIT:
From reading your logs, your form is being processed as JS. If you want the redirect, then remove the remote=>true from your form for now
The rest is my original answer, it may be an idea to implement some of it anyway:
I think you need redirect_to success_path
UPDATE:
ok, this will work, (hopefully):
1- in routes.rb do the following:
resources :users do
member do
get 'success'
end
end
In User controller, create a new action called success
def success
#user = User.find(params[:id])
end
In Create action
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to success_user_path(#user) }
else
format.html { render :new }
format.js { render :form_errors }
end
end
Then create /views/users/success.html.erb (or haml) and put whatever you want in it

Resources