Remote form submission not working with namespace on Rails3 - ruby-on-rails-3

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?

Related

Show validation errors in view for custom Devise login form

At the moment, I'm building a Rails platform using Spree Commerce where I need two Devise login forms.
The default Devise login forms are already implemented. For the second (custom) Devise form, I created the following view:
global_login.html.erb
<div class="inner">
<h2 class="large-margin">
Login
</h2>
<%= form_for(Spree::User.new, :url => spree_login_path) do |f| %>
<%= f.error_messages %>
<%= f.email_field :email, {:value => params[:email], :placeholder => 'Email address', :class => 'input'} %>
<%= f.password_field :password, {:placeholder => 'Password', :class => 'input'} %>
<%= f.submit 'Login', :class => 'submit' %>
<% end %>
</div>
This view does display a Devise login form. However, every time I submit the form, I'm being redirected to the default Devise view where the validation errors are shown.
Does somebody know a solution to this problem?
Cheers!
I found a workaround for this issue. I added a param to one of the forms named "inside_cart". When the request is send to the #create method, I check if the param is present.
I overwrote the Spree::UserSessionsController#create method as follows:
Spree::UserSessionsController.class_eval do
layout false
def create
authenticate_spree_user!
if spree_user_signed_in?
respond_to do |format|
format.html {
flash[:success] = Spree.t(:logged_in_succesfully)
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
}
format.js {
user = resource.record
render :json => {:ship_address => user.ship_address, :bill_address => user.bill_address}.to_json
}
end
else
if params[:spree_user] && params[:spree_user][:inside_cart]
flash.now[:error] = t('devise.failure.invalid')
render :new, layout: false
else
flash.now[:error] = t('devise.failure.invalid')
render 'spree/users/global_login', layout: 'spree/layouts/spree_application'
end
end
end
end

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 :)!

form_for tag with conditions + rails3

I have a form_for tag and I want to enable its :remote => true option upon the user status
if the current user is not an admin I want the form to be :remote => true
if the current user is an admin I want the form to be a default form
This is what I came up with and its nowt working ;(
<%= form_for #school, ((:remote => true) unless current_user.admin?) , :html => { :class => 'form-horizontal' } do |f| %>
can some one help me
I'm on
ruby 1.9
Rails 3.2.x
thanks in advance
How about using a Helper method with the following
def form_remote_or_not(user, object, &block)
if user.admin?
form_for object, :remote => true, &block
else
form_for object, &block
end
end
As for your view file; <%= form_remote_or_not(current_user, #school) do |f| %>

Rails 3 Route error - "No Route Matches"

I've been reading over this resource as well as this post to try to understand Routes more (currently learning programming/Rails by doing) but am wondering how I can fix the error I'm getting, which is No route matches {:controller=>"profiles", :action=>"show"}.
I get the error working my way through a Rails 3 sign-up process using nested model forms. The sign-up process, as follows:
user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.save
user.save
The sign-up process starts at the homepage with the following form:
<%= form_for :user, :url => signup_path, :html => {:id => 'homepage'} do |f| %>
<div>
...
</div>
<%= f.fields_for :profile do |f| %>
<% end %>
<% end %>
Then the process goes to fill in the profile, then redirect to the new User's profile after this form is completed:
<%= form_for :profile, :html => { :multipart => true } do |f| %>
<div>
...
</div>
<%= f.fields_for :user do |f| %>
<% end %>
<% end %>
I have accepts_nested_attributes_for :user and :profile in their respective models.
My rails server it gives me a bit more detail:
ActionController::RoutingError (No route matches {:controller=>"profile.save", :action=>"show"}):
app/controllers/profiles_controller.rb:15:in `create'
So in my ProfilesController in 'create':
def create
#profile = Profile.new(params[:profile])
if #profile.save
redirect_to profile_path, :notice => 'User successfully added.'
else
render :action => 'new'
end
end
Seems clear that the issue is in profile_path, so my Routes.rb:
post "/signup" => "profiles#create", :as => "signup"
match "skip/signup", :to => "info#signupskip"
match "skip/profiles/new", :to => "profiles#newskip"
root :to => "users#create"
Can anyone help shed light on what I'm doing wrong/missing in my Routes.rb file?
The redirect path should contain the specific profile to redirect to:
if #profile.save
redirect_to profile_path(#profile), :notice => 'User successfully added.'
else
.....
Also the routes should include this line:
get "/profiles/:id" => "profiles#show", as => "profile"