Rails 3.0.12 remote form failing with AJAX update - ruby-on-rails-3

Using Ruby 1.8.7-p358, Rails 3.0.12, gem responder, gem simple-form; jquery_ujs.js
I have a form where :remote=>true and also uploading a file
= simple_form_for :resume, :url => {:controller => :resumes,
action => :upload_resume, :id => #job_seeker.id}, :html => { :multipart => true }, :remote => true do |f|
etc
The Resume controller method looks like this:
respond_to :html, :xml, :json, :js
def upload_resume
r = #job_seeker.resumes.new
r.name = params[:resume][:name]
r.source_path = params[:resume][:content].original_filename
r.doc_type = params[:resume][:content].content_type
r.content = params[:resume][:content].tempfile.read
r.size = r.content.size
r.source_ip = request.remote_ip
r.save
,end
which it does, but will not respond to upload_resume.js.erb, which looks like this:
$('#resume_list').html("<%= escape_javascript(render(:partial => 'resumes/resumes' ))%>");
Is there anything about data-remote & file uploads that causes the render to misbehave?
Any help would be appreciated.

solved the issue by converting erb to haml

Related

Rails paperclip params not being sent when updating user

I am installed paperclip, but no params are being sent. Here is what I have...
#app/views/users/edit.html.erb
<%= form_for #user, :url => users_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
#app/models/user.rb
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/images/:style/missing.png"
app/controllers/users_controller.rb
def update
#user = current_user
respond_to do |format|
if #user.update_attributes(params[:user])
...
Any ideas? Not sure where to go next. I don't even see 'avatar' in my params hash.
At a glance the code you have there looks OK. Can you paste in the request Logs located in the console, or logs/development.log. It should help give some insight to the issue.
I wrote an article Uploading Files to S3 in Ruby with Paperclip on the Heroku dev center. The repo has example code on how to set up Paperclip. Might help to reference that?
I needed to add the code...
<%= f.file_field :avatar %>
to my app/views/users/_form.html.erb file rather than app/views/users/edit.html.erb

Do I need to reload the comment object or the partial that contains the comment in case of using Ajax in Rails 3?

I have been trying to figure out adding comments without reloading the page using Ajax, after reading few different tutorials this is what I came up to so far, and it's not working:
inside user_comments/_comments.html.erb
<div id="comment_form">
<%= simple_form_for [#commentable, #comment], :html => { :multipart => true }, :remote => true do |f| %>
<div class="picture"><%= image_tag current_user.avatar.url(:thumb) %></div>
<%= f.input :content, label: false, :placeholder => "Add Comment", :input_html => { :rows => 4 } %>
<%= f.submit "Add Comment" %>
<% end %>
</div>
Inside the controller:
def create
#users = User.all
#comment = #commentable.user_comments.new(params[:user_comment])
#comment.user_id = current_user[:id]
##commentable.user_comments.create(:user_id => current_user[:id])
if #comment.save
flash[:notice] = "Successfully created comment."
respond_to do |format|
format.html { redirect_to #commentable }
format.js
#format.js #{ render 'create.js.erb' }
end
else
render :new
end
end
and inside the create.js.erb
// Display a Javascript alert
<% if remotipart_submitted? %>
$("#comments_list").append("<%= escape_javascript(render(:partial => 'user_comments/comments')) %>");
<% end %>
I'm using a Gem called: remotipart
I don't know what I'm missing in the process.
in the console I get:
POST http://localhost:3000/assignments/2/user_comments
200 OK
134ms
which means the post goes through, but the comment doesnt get added back to the partial.
Ok after 2 days! I fixed it, here is what I can share and might help:
1- make sure to include the :remote => true to the form that is about to be submitted
2- Check the controller and see what the Create action is being redirected, in my case I changed to this:
def create
#users = User.all
#comment = #commentable.user_comments.new(params[:user_comment])
#comment.user_id = current_user[:id]
##commentable.user_comments.create(:user_id => current_user[:id])
if #comment.save
flash[:notice] = "Successfully created comment."
respond_to do |format|
format.html
format.js {#comments = #commentable.user_comments}
end
else
render :new
end
end
Then make sure the create.js.erb is written properly:
$("#comments_list").empty()
$("#comments_list").append("<%= escape_javascript(render(:partial => 'comments')) %>");
there you go! I hope some creates a proper tutorial for newbies like me :)!

render partial from controller with :layout => !request.xhr? returns missing template?

I cloned and tweak this rails app and I am experiencing some trouble with rendering a partial when on ajax request, in the logs I see that the guilty line is in the registrations_controller.rb (Devise)
class RegistrationsController < Devise::RegistrationsController
def create
build_resource
if resource.save
if resource.active_for_authentication?
sign_in(resource_name, resource)
(render(:partial => 'thankyou', :layout => false) && return) if request.xhr?
respond_with resource, :location => after_sign_up_path_for(resource)
else
resource.update_attribute(:encrypted_password, nil) # make sure there is no password
expire_session_data_after_sign_in!
(render(:partial => 'thankyou', :layout => false) && return) if request.xhr?
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
render :partial => 'email_capture', :action => :new, :layout => !request.xhr?**
end
end
protected
def after_inactive_sign_up_path_for(resource)
'/thankyou.html'
end
def after_sign_up_path_for(resource)
redirect_to root_path
end
end
The error message returned is this:
ActionView::MissingTemplate - Missing partial
with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder,
:coffee, :haml]}. Searched in: *
"/Users/Davide/Documents/Jobsite/rails-prelaunch-signup-1click/app/views"
* "/Users/Davide/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/devise_invitable-1.1.8/app/views"
* "/Users/Davide/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/devise-2.2.4/app/views"
Interestingly if I remove the :layout => !=request.xhr? parameter the partial gets found but the page refreshes and the loses all the stylesheets and other assets.
Any idea where should I start looking?
Thanks!
I have also cloned and modified that project, and ran into nearly the same issue. For me, the problem would appear after the second failure of saving a bad email address--but the result was identical.
By using context and information from the following question/answer:
form returned from AJAX request is not recognized as 'remote' when submitted
I was able to make a change that resolved my issue. In _email_capture.html.erb, add :remote => true, to the simple_form_for line. In my case, the whole revised line is:
<%= simple_form_for resource, :as => resource_name, :remote => true, :url => registration_path(resource_name) , :html => {:class => 'form-inline'} do |f| %>
Hope this helps you, too!

Remote form submission not working with namespace on Rails3

I am working on a project which allow people to upload a personal profile, including projects and updates related to their project.
Everything is working fine except that I would like to submit my form using :remote => true but i have no way to get it to work. I could use classic jquery/ajax to submit the form, but the Rails3 UJS is great !
updates_controller.rb
def create
#update = Update.new(params[:update])
#update.user_id = current_user.id
#update.project_id = params[:project_id]
if #update.save
respond_to do |format|
format.js { render :nothing => true }
end
end
end
_form.html (views)
<%= form_for([#project, #update], :url => profile_project_updates_path, :remote => true) do |f| %>
I tried also
<%= form_for([:profile, #project, #update], :remote => true) do |f| %>
routes.rb
namespace :profile do
resources :projects, :only => [:show, :index, :edit] do
resources :updates
end
end
If anyone has an idea how to get the :remote => true to work here , it will be great !!
Thanks to all the Stack Overflow community for all the precious resources I found here so far.
_Clement
Did you initialized #project in new action of controller?
Try to replace
<%= form_for([#project, #update], :url => profile_project_updates_path, :remote => true) do |f| %>
with
<%= form_for([#update.project_id, #update], :url => profile_project_updates_path, :remote => true) do |f| %>
What is the server response when you submit the form?

how do specfiy :with attribute in rails 3 link_to

This is link_to_remote code for rails 2.3.8
link_to_remote "Click here",
:update=>'flowtracker',
:with=>"'topcount='+$('topcount').value,
:url=>{
:controller => 'sessions',
:action => 'session_for_tracker',
:layout=>1
}
link_to_remote deprecated in rails 3
so how can i specify the :with attribute in link_to rails 3
And i'm using prototype.js
Use link_to helper with :remote => true option and do whatever you want in your js view file to your action.
EXAMPLE
in view:
<%= link_to 'ajax call', :controller => "sessions", :action => "session_for_tracker", :remote => true %>
in controller:
def session_for_tracker
#topcount = 1000; #get actual value here
respond_to do |format|
format.js
end
end
in js view for action:
page << %|
$('flowtracker').update("#{#topcount}");
|