Rails remote form submit not working - ajax

I'm trying to submit a form via ajax/js. When I set :remote => true it still submits via html.
<%= form_for(#message), :remote => true, do |f| %>
<%= f.label :Note_To_Doctor %>
<%= f.text_field :note %>
<%= f.collection_select(:physician_id, Physician.all, :id, :physician_name )%>
<%= f.button :send, :class => 'btn btn-info' %>
<% end %>
I could have sworn I've done it like this. Do I need to create a create.js.erb and if so, what should go inside of it. Also, what should I put inside the create action as a respond block?
controller:
def create
#message = Message.new(params[:message])
if #message.save
MessageMailer.paging(#message).deliver
respond_to do |format|
format.html { redirect_to messages_path, flash.notice = "Message was sent" }
format.js { render "index" }
end
else
#message = Message.new(params[:message])
end
end
index.js.erb
$("#messages").html("<%= escape_javascript render("messages") %>");

Just replace form_for with,
<%= form_for(#message, :remote => true) do |f| %>

In your case the :remote=>true is not working you should try this solution. Submit form in rails 3 in an ajax way (with jQuery)

Related

Issue with paperclip, upload image

I have some problem with paperclip gem. When I try add record in my app i got error : http://postimg.org/image/xy0stdctd/
When i add record without image upload, everything works great.
controller
def create
#user = User.new
end
def made
#user = User.new(user_params)
if #user.save
redirect_to(action:'index' )
else
render ('index')
end
end
def user_params
params.require(:user).permit(:avatar, :name)
end
view
<%= form_for :user, url: {action: 'made'}, :html => { :multipart => true } do |form| %>
<%= form.text_field :name %>
<br>
<%= form.file_field :avatar %>
<br>
<%= form.submit "dodaj" %>
<% end %>
in terminal I have
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (8.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/
layout (130.0ms)
Cannot render console with content type multipart/form-dataAllowed content types: [#, #, #]

Can not create user with Rails app on heroku, but works perfect on localhost

So I am working through the Michael Hartl tut and this app works perfectly on the localhost but the moment I deploy to heroku it wont create a user when i submit the information. In fact it just sits there as if I just clicked on an empty screen, no error message nor a rediret. I looked at the heroku logs and there are no exceptions that I can see being logged. I tried updating the controller behavior but i get the same result. This is frustrating.
my form looks like this:
<div class="main-form">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages'%>
<%= f.label :name %>
<%= f.text_field :name, class: "active" %><br/>
<%= f.label :email %>
<%= f.text_field :email %><br/>
<%= f.label :password %>
<%= f.password_field :password %><br/>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %><br/>
</div>
<div class="actions">
<%= f.submit "create my account", class: "btn btn-lg btn-default" %>
</div>
<% end %>
my controller looks like:
class UsersController < ApplicationController
def new
end
def show
#user = User.find(params[:id])
#title = #user.name
end
def create
#title = "welcome"
#user = User.new(user_params)
if #user.password_confirmation.empty? == false
#user.save
redirect_to user_path(#user)
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
i have also tried setting up my create method like:
def create
#user = User.new(user_params)
if #user.save
redirect_to #user
else
render 'new'
end
end
neither of these methods worked? Any advice would be welcome.
If you inspect the page it looks like your submit button is outside of the form definition:

skipping(going straight to the show page) the index page if there is only 1 item in the database

How can I skip the index page and go straight to the show page if there is exactly only 1 project update in my database for that project(while also making sure no button gets displayed if there is no update(zero) in the database?
I tried this:
<% if #project.updates.any? %>
<%= button_tag type: "button", :class => "radius" do %>
<% if #project.updates=1 %>
<%= link_to 'Project Update', project_update_path(#project), :style => "color: white" %>
<% else %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
<% end %>
<% end %>
but i get this error:
undefined method `each' for 1:Fixnum
On this line:
<% if #project.updates=1 %>
What is the proper syntax for this?
Below is the relevant code:
My button:
<% if #project.updates.any? %>
<%= button_tag type: "button", :class => "radius" do %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
<% end %>
These is my custom route:
get 'all_project_updates/:id' => 'project_updates#index', as: 'all_project_updates'
These are the final generated routes:
project_updates_path GET /project_updates(.:format) project_updates#index
project_update_path GET /project_updates/:id(.:format) project_updates#show
This is my projects controller(show action)
def show
#project = Project.find(params[:id])
#comments = Comment.all.where(:project_id => #project.id)
#updates = ProjectUpdate.all.where(:project_id => #project.id)
end
And this is my project updates controller index action:
def index
#projectUpdates = ProjectUpdate.where(:project_id => params[:id])
respond_to do |format|
format.html
end
end
And this is my project updates controller show action:
def show
#projectUpdate = ProjectUpdate.find(params[:id])
respond_to do |format|
format.html
end
end
You probably meant:
<% if #project.updates.count == 1 %>
== is for comparison, = is usually for assignment. Also, you need to compare updatesnumber to1(you can get number withcountmethod), notupdates` themselves.
Instead of comparison, you can use Enumerable#one? method:
<% if #project.updates.one? %>

only showing button to index action when there are objects listed in the index(in the database)

I have a button that links to the projects updates index action through all_project_updates_path i set in my routes. Here is my button code:
<%= button_tag type: "button", :class => "radius" do %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
I want this button to only be visible when there are project updates in the database. Otherwise I want this button to dissapear. I tried:
<% if all_project_updates_path(#project) != nil %>
<%= button_tag type: "button", :class => "radius" do %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
<% end %>
And also
<% if #updates != nil %>
<%= button_tag type: "button", :class => "radius" do %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
<% end %>
but that doesn't seem to work. Looking for a simple explanation as I am a relative beginner with ruby.
This is the route:
get 'all_project_updates/:id' => 'project_updates#index', as: 'all_project_updates'
This is my projects controller(show action)
def show
#project = Project.find(params[:id])
#comments = Comment.all.where(:project_id => #project.id)
#updates = ProjectUpdate.all.where(:project_id => #project.id)
end
And this is my project updates controller index action:
def index
#projectUpdates = ProjectUpdate.where(:project_id => params[:id])
respond_to do |format|
format.html
end
end
You’re examining a path helper, all_project_updates_path, when you need to be querying a model object. The all_project_updates_path call is a helper to return a path for linking between web pages.
all_project_updates_path(#project) # => /all_project_updates/1
So you’re really asking:
'/all_project_updates/1'.nil? # => false
Because it’s just a string, it won’t be nil.
Instead, you should be examining the project_updates directly. I’m not sure how your models are related, but assuming that a Project has_many :project_updates, try this:
if #project.project_updates.any?
That will return true if a #project has any updates.
Beyond your immediate question, I would recommend considering whether nested resources are a better fit for this usage. You could declare your routes like this:
resources :projects do
resources :project_updates
end
Then you would get project_project_updates_path(#project) and no longer need your custom route that pretty much duplicates that functionality.
Try this:
<% if #updates.any? %>
<%= button_tag ... %>
<% end %>
#updates is an empty collection (ActiveRecord_Relation to be precise) of ProjectUpdate objects if no records were found, it's not nil.

Like button does not update with AJAX

I am passing a collection (#feed_items) to a _feed_item partial via the :collection option and converting it to dailypost with :as => :dailypost.
Inside the _feed_item partial I rendered another partial for _like_button, and i used :locals to continue using dailypost.
Everything works fine with the database. Likes get added and taken out :) BUT
I am trying to use (AJAX) to create.js.erb & destroy.js.erb the like button.
For some reason only the top post gets updated correctly and i have to refresh the page to see the ones below it.
I know the solution involves assigning a unique post id to each "like" element, say "like-123", but that is where i am stuck.......
I also know that the problem may be in _feed_items.html.erb because i am passing two ids....any suggestions??
Views
_feed.html.erb
<% if #feed_items.any? %>
<ol>
<%= render partial: 'shared/feed_item', collection: #feed_items, :as => :dailypost %>
</ol>
<% end %>
_feed_items.html.erb
<li id="<%= #dailypost.id %>">
<span class="user">
<%= link_to dailypost.user.name, dailypost.user %>
<span class="content"><%= dailypost.content_html %></span>
<div id="like">
<%= render :partial => 'likes/like_button', :locals =>{:dailypost => dailypost} %>
</div>
</li>
_like_button.html.erb
<% if like = current_user.likes.find_by_dailypost_id(dailypost.id) %>
<%= form_for like, :html => { :method => :delete }, :remote => true do |f| %>
<%= f.submit "Unlike" %>
<% end %>
<% else %>
<%= form_for current_user.likes.build, :remote => true do |f| %>
<div><%= f.hidden_field :dailypost_id, value: dailypost.id %></div>
<%= f.hidden_field :user_id %>
<%= f.submit "Like" %>
<% end %>
<% end %>
create.js.erb
$("#like").html("<%= escape_javascript(render :partial => 'like_button', :locals => {:dailypost => #dailypost}) %>");
destroy.js.erb
$("#like").html("<%= escape_javascript(render :partial => 'like_button', :locals => {:dailypost => #dailypost}) %>");
Controller
class LikesController < ApplicationController
respond_to :html, :js
def create
#like = Like.create(params[:like])
#dailypost = #like.dailypost
respond_to do |format|
format.js
format.html { redirect_to :back }
end
end
def destroy
like = Like.find(params[:id]).destroy
#dailypost = like.dailypost
respond_to do |format|
format.js
format.html { redirect_to :back }
end
end
end
Hmmm.Replace the first-line in _feed_items.html.erb with this.
<li id="dailypost<%= dailypost.id %>">
and In create.js.erb and destroy.js.erb, make changes
$("#like").html(...)
To
$("#dailypost<%= dailypost.id%> #like").html(...)
This should Work.

Resources