Ajax function implementation in controllers in rails - javascript

So I've been searching for an answer to this, but I've been having a lot of trouble. I'm trying to have a form_for in rails that goes to the create action in the User controller, which updates the color of the submit button depending on whether it fails submitting or not. Here is my code, some of which is directly from a tutorial I found online:
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.js {}
format.json { render json: #user, status: :created, location: #user }
else
format.html { render action: "new" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
private
def user_params
params.require(:user).permit(:email)
end
And here is my create.js.coffee.erb file.
if(!isValidEmailAddress($('#e_submit').val())){
$('.button').text("Invalid Email");
$('.button').animate({"background-color":"#FFBF00"},300);
$('.button').hover(function(){
$(this).css("background-color","#FFBF00");
}, function(){
$(this).css("background-color","#FFBF00");
});
$('#e_submit').css("box-shadow","0 0 0px rgba(0,0,0,0)");
$('#e_submit').val("");
}
Basically, I want different js events to happen depending on the submitted information, and I don't know how to do this in rails.

You could check if there are any errors on #user in your js template to know if save is success or not, create.js.coffee.erb:
<% if #user.errors.empty? %>
# successfully saved
<% else %>
# failed to save
<% end %>

Related

400 Bad Response Error in AJAX Post Method

I'm trying to add a comment to a link (this is a mock Reddit application built with Rails and JavaScript/JQuery) through an AJAX request to avoid an entire page load (I can't use remote: true in this application).
I'm able to add comments and append them the list of comments through Rails, but when I try to use the AJAX method, I get a 400 Bad Request Error.
Here's my script:
`
function submitViaAjax() {
$("#new_comment_button").on("click", function (e) {
url = this.action
//var commentText = document.getElementById("comment_body").innerHTML
//var myJSON = JSON.stringify(commentText);
data = {
'authenticity_token': $("input[name='authenticity_token']").val(),
'comment': {
'content': $("#comment_body").val()
}
};
console.log(data);
$.ajax({
type: "POST",
url: url,
data: data,
headers: { 'Content-Type': 'application/json' },
success: function (response) {
var $ul = $("div.comments_section ul");
$ul.append(response)
}
})
e.preventDefault();
})
};
And here's my Links show page that has the form:
<div class="comments_section">
<%= render 'comments/comments' %>
</div>
<!--<div id="comments">
</div> -->
<%= simple_form_for [#link, Comment.new] do |f| %>
<div class="field">
<%= f.text_area :body, class: "form-control" %>
</div>
<br>
<%= f.submit "Add Comment", class: "btn btn-primary", id: "new_comment_button", data: { disable_with: false } %>
<% end %>
Any insight would be greatly appreciated.
UPDATE: My controller code, per request:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
if params[:link_id]
#link = Link.find(params[:link_id])
#comments = #link.comments
else
#comments = Comment.all
end
respond_to do |format|
format.html { render :index }
format.json { render json: #comments }
end
end
def create
#link = Link.find(params[:link_id])
#comment = #link.comments.new(comment_params)
#comment.user = current_user
respond_to do |format|
if #comment.save
format.html { redirect_to #link, notice: 'Comment was successfully created.' }
format.json { render json: #comment, status: :created, location: #comment }
render 'comments/show', :layout => false
else
format.html { render action: "new" }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_back fallback_location: root_path, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:link_id, :body, :user_id)
end
end
Echoing what #Taplar said, there does not look to be an action attribute for the #new_comment_button element; is this actually encoded somewhere? As well, that action attribute has to be the exact value of your AJAX 'service' - is this correctly set up?

Rails 5.1.2 Ajax remote: true not working

I would like to implement ajax to create a post, without recharging the page and not going to the show (I following a tutorial) but is not working.
in the _form.html.haml you can see remote:true in the form helper
= form_for #post,remote: true do |f|
- if #post.errors.any?
#error_explanation
%h2= "#{pluralize(#post.errors.count, "error")} prohibited this post
from being saved:"
%ul
- #post.errors.full_messages.each do |message|
%li= message
.mdl-textfield.mdl-js-textfield.full-width
= f.text_area :body, class:"mdl-textfield__input"
= f.label :body, "Comparte con la Comunidad", class:"mdl-
textfield__label"
.actions.text-right
= f.submit 'Publicar', class:"mdl-button mdl-js-button mdl-button--
raised mdl-button--colored"
however when the post is created, the application redirect to the show of the post created recently in html format, it should be stay in the form.
I don't know if the version of Rails 5.1.2 has a issue, but in Rails 4 with only typing remote: true the application stay in the form after the submit action.
post controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = current_user.posts.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:body)
end
end
also I have a show.js.erb file to render a view to see the body of the post after he is created.
show.js.erb
$('#posts').append("<%= render j #post %>")
_post.haml
%article
=post.body
Done! in the post controller is missing to respond a JS format and the show.js.erb the render method has bad syntax
1) post controller: add format.js
def create
#post = current_user.posts.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
format.js { render :show }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
format.js { }
end
end
end
2) show.js.erb: the J it should be before render
$("posts").append(<%= j render #post %>)

My rails form is not working with has_scope

(I have been coding for barely a month so apologies if it's a stupid question). I have a user model just with name, email and type. I have created an index form that you can filter by type and it should show the results.
Form and filters show as expected by I have 2 problems:
1. The usertype is duplicated. For example, if I have 5 users (created with the faker gem) each one of them is customer or supplier, the filter shows customer and supplier 5 times instead of twice
2. When I select a filter, it all results are shown and they are not filtered.
This is my model:
class User < ActiveRecord::Base
has_many :microposts
scope :by_userType, -> userType { where(:userType => userType) }
This is my Controller
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
has_scope :by_userType, type: :array #, :using => [:userType]
# GET /users
# GET /users.json
#def index
# #users = User.all
#end
def index
#users = apply_scopes(User).all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
#user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email, :userType)
end
end
This is my form:
<p id="notice"><%= notice %></p>
<h1>Listing Users</h1>
<%= form_tag users_path, method: 'get', id: "users_search" do%>
<% #users = User.all %>
<% #users.each do |user|%>
<%= check_box_tag "by_userType[]", user.userType %><%= user.userType %><br>
<% end %>
<%= submit_tag "submit" %>
<% end %>
<div id="users"><%= render 'user_list' %></div>
<script type="text/javascript">
$(function () {
$('input[type=checkbox]').change(function(){
$.get($('#users_search').attr('action'),
$('#users_search').serialize(), null, 'script');
return false;
});
$('users_search').submit(function() {
$.get(this.action, $(this).serialize(), null, 'script');
return false;
});
});
</script>
Thank you in advance for your time!
So a couple of things here. First of all, welcome to rails, and welcome to Stack Overflow.
+1 for asking a question and providing code examples for what you've done so far.
Please note that for standardization, case is important in rails. For declaration of scopes you should use snake case. UserModel is the class name, user_model would be snake case.
Now as far as the actual implementation, I would do it somewhat differently. If you notice most of the ajax-filtering of one model based on a field doesn't use the same model as the parameter, it's not hte ONLY way, but I prefer it, as it allows flexibility for adding extra fields to user_type later. Meaning, if you extract user type into it's own model, then you can easily filter your users from the user_type attribute of :name.
So your user model would have:
class User < ActiveRecord::Base
has_many :microposts
belongs_to :user_type
scope :by_user_type, -> user_type { where(:user_type => user_type) }
end
Then create a new model called user_model
rails g scaffold user_model name:string slug:string avatar:string
** Note that the additional fields are just examples, name is hte only necessary one. But if you want to let users do url searches, the slug is easy to use. i.e. yoursite.com/user_type/sellers
Now create a migration to remove your existing userType field in users and a new one for the relationship.
rails g migration modify_user_type_in_user
And the contents of that file would be
class ModifyUserTypeInUser < ActiveRecord::Migration
def change
remove_column :users, :userType
add_reference :users, :user_type, index: true
end
end
Now edit the new user_type.rb model and add the relationship for users
class UserType < ActiveRecord::Base
has_many :users
end
You also need to use UJS, which you didn't mention in your post. When your form field is clicked on, it's sending a javascript (ajax) request. This means that in order to change the data, you'll need a javascript response template.
So add the file app/views/users/index.js.erb and put inside it these contents:
$("#users").html('<%= j(render("user_list", users: #users)) %>');
$("table tbody tr:nth-of-type(even)").css("background","#BD9FB1");
Lastly, you'll need to change your form, so it represents the correct searchable model. So edit 'app/views/users/index.html.erb'
<p id="notice"><%= notice %></p>
<h1>User Filter</h1>
<%= form_tag users_path, method: 'get', id: "users_search" do%>
<% #user_types = UserType.all %>
<% #user_types.each do |user_type|%>
<%= check_box_tag "by_user_type[]", user_type.id %><%= user_type.name %><br>
<% end %>
<%= submit_tag "submit" %>
<% end %>
<div id="users"><%= render 'user_list' %></div>
<script type="text/javascript">
$(function () {
$('input[type=checkbox]').change(function(){
$.get($('#users_search').attr('action'),
$('#users_search').serialize(), null, 'script');
return false;
});
$('users_search').submit(function(e) {
e.preventDefault();
$.get(this.action, $(this).serialize(), null, 'script');
return false;
});
});
</script>

Rails: Updating quantities in shopping cart

Ruby newbie here. I'm going through Agile Web Development With Rails. In chapter 11 it challenges you to add a 'decrease quantity' button to items in the shopping cart. I went ahead and tried to implement an increase link as well.
The problem is it's not doing anything when I click on the links.
line_items_controller.rb
def decrease
#cart = current_cart
#line_item = #cart.decrease(params[:id])
respond_to do |format|
if #line_item.save
format.html { redirect_to store_path, notice: 'Item was successfully updated.' }
format.js { #current_item = #line_item }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #line_item.errors, status: :unprocessable_entity}
end
end
end
def increase
#cart = current_cart
#line_item = #cart.increase(params[:id])
respond_to do |format|
if #line_item.save
format.html { redirect_to store_path, notice: 'Item was successfully updated.' }
format.js { #current_item = #line_item }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
cart.rb
def decrease(line_item_id)
current_item = line_items.find(line_item_id)
if current_item.quantity > 1
current_item.quantity -= 1
else
current_item.destroy
end
current_item
end
def increase(line_item_id)
current_item = line_items.find(line_item_id)
current_item.quantity += 1
current_item
end
routes.rb
resources :line_items do
put 'decrease', on: :member
put 'increase', on: :member
end
_line_item.html.erb
<% if line_item == #current_item %>
<tr id="current_item">
<% else %>
<tr>
<% end %>
<td><%= line_item.quantity %> ×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
<td><%= link_to "-", decrease_line_item_path(line_item), method: :put, remote: true %></td>
<td><%= link_to "+", increase_line_item_path(line_item), method: :put, remote: true %></td>
<td><%= button_to 'Remove Item', line_item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
/line_items/increase.js.erb
$('#cart').html("<%= escape_javascript(render(#cart)) %>");
$('#current_item').css({'background-color':'#88ff88'}).animate({'background-color':'#114411'}, 1000);
/line_items/decrease.js.erb
$('#cart').html("<%= escape_javascript(render(#cart)) %>");
$('#current_item').css({'background-color':'#88ff88'}).animate({'background-color':'#114411'}), 1000);
if ($('#cart tr').length==1) {
// Hide the cart
$('#cart').hide('blind', 1000);
}
Let me know if I forgot anything crucial. Thanks in advance!
----EDIT----
I changed the code to what Rich posted, and this is what shows up in the console when I click the '+' link.
Started GET "/line_items/25/qty" for ::1 at 2016-01-30 23:49:11 -0600
ActionController::RoutingError (No route matches [GET] "/line_items/25/qty"):
So I see that it needs a route for qty but I'm not quite sure how to set that up. I'm guessing the JS alert we set up isn't firing because it's snagging up at this point?
----EDIT 2----
Now I'm passing the links as POST and getting this name error, from both the up and down links:
Started POST "/line_items/25/qty" for ::1 at 2016-01-31 09:49:04 -0600
Processing by LineItemsController#qty as JS
Parameters: {"id"=>"25"}
Completed 500 Internal Server Error in 38ms (ActiveRecord: 0.0ms)
NameError (undefined local variable or method `current_cart' for #<LineItemsController:0x007fbfb11ea730>):
app/controllers/line_items_controller.rb:70:in `qty'
What confuses me here is that current_cart works in the increase and decrease methods but not in qty.
Some time passed since you asked this - did you solved it? I have one answer to this issue - I managed to increase/decrease numbers in cart, but without Ajax calls - everytime button clicked - page reloads (I removed remote: true). I have not followed #Richard Peck solution, even dough it might be a better way. So, as he pointed out, routes had to use get method, instead of put (I am not very sure why get instead of post should be used, but if Rails asks it I kindly do it):
resources :line_items do
get 'decrease', on: :member
get 'increase', on: :member
end
Also, I have been noticed by other guy on StackExchange that i forgot to allow increase / decrease actions in controller, since we restrict most of them by default:
line_items_controller.rb:
before_action :set_cart, only: [:create, :decrease, :increase]
So, I guess these two issues was your problem (just like mines).
line_items_controller.rb:
def decrease
product = Product.find(params[:product_id])
#line_item = #cart.remove_product(product)
respond_to do |format|
if #line_item.save
format.html { redirect_to cart_path, notice: 'Line item was successfully updated.' }
format.js
format.json { render :show, status: :ok, location: #line_item }
else
format.html { render :edit }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
def increase
product = Product.find(params[:product_id])
#line_item = #cart.add_product(product)
respond_to do |format|
if #line_item.save
format.html { redirect_to :back, notice: 'Line item was successfully updated.' }
format.js
format.json { render :show, status: :ok, location: #line_item }
else
format.html { render :edit }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
cart.rb:
def add_product(product)
current_item = line_items.find_by(product_id: product.id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product.id)
end
current_item
end
def remove_product(product)
current_item = line_items.find_by(product_id: product.id)
if current_item.quantity > 1
current_item.quantity -= 1
elsif current_item.quantity = 1
current_item.destroy
end
current_item
end
and links:
<td><%= link_to "-", decrease_line_item_path(product_id: line_item.product), class:"btn btn-danger" %></td>
<td><%= link_to "+", increase_line_item_path(product_id: line_item.product), class:"btn btn-success" %></td>
Would this make what you need? And maybe any ideas why Ajax is not working? Here a link to full my question:
https://stackoverflow.com/questions/40551425/rails-ajax-jquery-cant-make-to-render-changes-without-page-refresh
With this type of pattern, you're best DRYing up your logic into a single action:
#config/routes.rb
resources :line_items do
match :qty, action: :qty, via: [:post, :delete], on: :member #-> url.com/line_items/qty
end
#app/models/line_item.rb
class LineItem < ActiveRecord::Base
after_update :check_qty, if: "qty_changed?"
private
def check_qty
self.destroy if self.qty.zero?
end
end
#app/controllers/line_items_controller.rb
class LineItemsController < ApplicationController
def qty
#cart = current_cart
#item = #cart.line_items.find params[:id]
if request.post? #-> increment
method = "increment"
elsif request.delete? #-> decrement
method = "decrement"
end
#item.send(method, :qty, params[:qty])
respond_to do |format|
if #item.save
format.html { redirect_to store_path, notice: 'Item was successfully updated.' }
format.js { #current_item = #line_item }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #line_item.errors, status: :unprocessable_entity}
end
end
end
end
This will allow you to pass a single link (with the potential of qty) to your controller. If you leave it blank, it will just use 1 as the qty:
<%= button_to "+", line_items_qty_path(#line_item), params: { qty: 5 } %>
<%= link_to "-", line_items_qty_path(line_item), method: :post, remote: true %>
<%= link_to "+", line_items_qty_path(line_item), method: :delete, remote: true %>
Debugging
Since you're new, you need to understand about debugging.
When doing something like this, there are many places it could "go wrong". Like many inexperienced devs, you've basically said "it's not working"... the problem is that many experienced devs know that there has to be a problem somewhere.
The best thing you can do is find out where it's going wrong. This is a tedious process (test each part); you should start with your JS:
#app/views/line_items/qty.js.erb
alert("test");
If the above fires, it means you're doing everything right up to that point.
If you add the above file with my recommended code, we'll have a much better idea as to what the problem may be. You'll also want to post your console logs for the requests sent to your line_items controller (this will indicate whether Rails treats the request as successful).
Once you've found the problem, you can then pinpoint what needs to be done to fix it, which is where many people expect a question to be based.
As an aside, we've built a cart before (it uses sessions rather than db):
I could write up how to do it if you want. It uses a session-based model.
Update
The error you're seeing is because you're sending a GET request through your link; my routes were POST & DELETE respectively. You need something like the following:
<%= link_to "-", line_items_qty_path(line_item), method: :post, remote: true %>
I think I got it wrong in my post (apologies) - you have to make sure you're passing the method as POST or DELETE
Update 2
To initialize the current_cart method, you need to make sure you have it available.
Ryan Bates alludes to this in his "Session Model" Railscast --
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_filter :current_cart
helper_method :current_cart
private
def current_cart
#current_cart = session[:cart_id] ? Cart.find(session[:cart_id]) : Cart.create
session[:cart_id] = #current_cart.id if #current_cart.new_record?
end
end

Agile Web Development with Rails: Two (2) items added to depot cart instead of one (1)

So I worked through the Agile Web Development with Rails book, and had a nice functional web application running on Heroku based on the "Depot" app in the book. I was editing CSS/SASS primarily, and I did something that messed up my line_item quantity in my cart.
For the life of me I can't figure out what I've done. Now when I click "Add to cart" button on my main page, 2 items are added to my cart. Does this have something to do with my database? I'm totally flummoxed after losing a bit of my flow in photoshop/CSS-land.
So in a nutshell:
I hit the "Add to cart" button, and I get two of each item I click on.
Here is my carts controller:
class CartsController < ApplicationController
skip_before_filter :authorize, only: [:create, :update, :destroy]
# GET /carts
# GET /carts.json
def index
#carts = Cart.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #carts }
end
end
# GET /carts/1
# GET /carts/1.json
def show
begin
#cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid cart'
else
respond_to do |format|
format.html #show.html.erb
format.json { render json: #cart }
end
end
end
# GET /carts/new
# GET /carts/new.json
def new
#cart = Cart.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #cart }
end
end
# GET /carts/1/edit
def edit
#cart = Cart.find(params[:id])
end
# POST /carts
# POST /carts.json
def create
#cart = Cart.new(params[:cart])
respond_to do |format|
if #cart.save
format.html { redirect_to #cart, notice: 'Cart was successfully created.' }
format.json { render json: #cart, status: :created, location: #cart }
else
format.html { render action: "new" }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# PUT /carts/1
# PUT /carts/1.json
def update
#cart = Cart.find(params[:id])
respond_to do |format|
if #cart.update_attributes(params[:cart])
format.html { redirect_to #cart, notice: 'Cart was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
#cart = current_cart
#cart.destroy
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_url }
format.json { head :no_content }
end
end
end
Here is my models/cart.rb file:
class Cart < ActiveRecord::Base
attr_accessible :title, :body, :name, :quantities_attributes, :quantities, :quantity, :product_id, :line_items
#the addition of everything in attr_accessible after :body may be unnecessary if bugs show up later on...
has_many :line_items, dependent: :destroy
def add_product(product_id)
current_item = line_items.find_by_product_id(product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id)
end
current_item
end
def total_price
line_items.to_a.sum { |item| item.total_price }
end
end
Here is my create.js.erb file
$("#notice").hide();
if ($('#cart tr').length == 1) { $('#cart').show('blind', 1000); }
$('#cart').html("<%=j render #cart %>");
$('#current_item').css({'background-color':'#fb0f19'}).
animate({'background-color':'#08BBD1'}, 1000);
Here is my application_controller.rb:
class ApplicationController < ActionController::Base
before_filter :authorize
protect_from_forgery
private
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
protected
def authorize
unless User.find_by_id(session[:user_id])
redirect_to login_url, notice: "Please log in"
end
end
end
Here is my views/carts/_cart.html.erb
<% unless cart.line_items.empty? %>
<div class="cart_title">Your Comic Cart</div>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= cart.total_price %> BTC</td>
</tr>
</table>
<%= button_to "Checkout", new_order_path, method: :get %>
<%= button_to 'Empty cart', cart, method: :delete,
confirm: 'Are you sure?' %>
<% end %>
line_items_controller.rb:
class LineItemsController < ApplicationController
skip_before_filter :authorize, only: :create
# GET /line_items
# GET /line_items.json
def index
#line_items = LineItem.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #line_items }
end
end
# GET /line_items/1
# GET /line_items/1.json
def show
#line_item = LineItem.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #line_item }
end
end
# GET /line_items/new
# GET /line_items/new.json
def new
#line_item = LineItem.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #line_item }
end
end
# GET /line_items/1/edit
def edit
#line_item = LineItem.find(params[:id])
end
# POST /line_items
# POST /line_items.json
def create
#cart = current_cart
product = Product.find(params[:product_id])
#line_item = #cart.add_product(product.id)
respond_to do |format|
if #line_item.save
format.html { redirect_to store_url }
format.js { #current_item = #line_item }
format.json { render json: #line_item,
status: :created, location: #line_item }
else
format.html { render action: "new" }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
# PUT /line_items/1
# PUT /line_items/1.json
def update
#line_item = LineItem.find(params[:id])
respond_to do |format|
if #line_item.update_attributes(params[:line_item])
format.html { redirect_to #line_item, notice: 'Line item was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
#line_item = LineItem.find(params[:id])
#line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url }
format.json { head :no_content }
end
end
end
I'm not sure if there are more files worth showing, it is late and I'm super lost, so I probably should have waited until morning to post this. Let me know if anyone else has run into this. I still have a hard time with databases, so I could be completely missing the right place to be looking. Thanks for any help, I know this misstep lacks focus in my explination.
I think the problem is in your store.js.coffee. Check and modify the code as per errata 153 from below link:
http://pragprog.com/titles/rails4/errata
i.e change $(document).on "ready page:change" to $(document).on "ready, page:change"

Categories

Resources