I try to use breadcrumbs_on_rails, but each time getting error:
syntax error, unexpected '(', expecting keyword_end add_breadcrumb "City", :doctors_city_sort_path(params[:city_id])
In controller:
def sort_by_city
#doctors = Doctor.where('city_id = ?', params[:city_id]).
paginate(page: params[:page], :per_page =>10)
add_breadcrumb "City", :doctors_city_sort_path(params[:city_id])
render 'index'
end
In routes.rb:
get '/doctors/city/:city_id',
to: 'doctors#sort_by_city', as: 'doctors_city_sort'
Please help to find decision for this task
You have a colon add_breadcrumb call, try this:
def sort_by_city
#doctors = Doctor.where('city_id = ?', params[:city_id]).
paginate(page: params[:page], :per_page =>10)
add_breadcrumb "City", doctors_city_sort_path(params[:city_id])
render 'index'
end
I found the decision! It's look like:
#city = City.find(params[:city_id])
add_breadcrumb "#{#city.name}", "#{#city.id}"
Related
class CartItemsController < ApplicationController
before_filter :initialize_cart, :check_not_signedin
def create
product = Product.find(params[:product_id])
kart = initialize_cart
qty = CartItem.select(:quantity).where(:cart_id => kart.id, :product_id => product.id)
if qty == 0
#item = CartItem.new(:cart_id => kart.id, :product_id => product.id, :quantity => qty+1)
if #item.save
flash[:success] = "Product added"
redirect_to category_products_path
end
else
if CartItem.where("cart_id = ? AND product_id = ?", kart.id, product.id).first.update_column(:quantity, qty+1)
flash[:success] = "Product updated"
redirect_to category_products_path
end
end
end
When I am trying to run this I'm getting the following error
"Can't convert FixNum into Array"
app/controllers/cart_items_controller.rb:17:in `create'
Please help!
The following line should return a ActiveRecord::Relation into qty:
qty = CartItem.select(:quantity).where(:cart_id => kart.id, :product_id => product.id)
You should use qty.count instead: qty.count == 0
Also, you can't add a ActiveRecord::Relation with 1 like this: qty+1. It will give you the error message you had.
I'm not sure what you are trying to do, but I suggest you use the debugger gem to help you troubleshoot your problem. Follow the guide here to setup, it's very simple to setup: http://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debugger-gem
Then, place debugger in your code:
product = Product.find(params[:product_id])
kart = initialize_cart
qty = CartItem.select(:quantity).where(:cart_id => kart.id, :product_id => product.id)
debugger # <---- here
if qty == 0
#item = CartItem.new(:cart_id => kart.id, :product_id => product.id, :quantity => qty+1)
if #item.save
Then you can find out more while you stopped at the debugger breakpoint, you can do stuff like:
qty.class
qty.count
# etc
Also, you can run rails console for testing.
I'm guessing that the following line is returning an Array:
CartItem.select(:quantity).where(:cart_id => kart.id, :product_id => product.id)
If this is the case then you can't simply add +1 to it on this line:
if CartItem.where("cart_id = ? AND product_id = ?", kart.id, product.id).first.update_column(:quantity, qty+1)
If this is not the case, can you point out which line is number 17 as pointed out in the error message.
In model:
def self.get_by_slug(slug)
self.where("slug = ?", slug)
end
In controller:
#route: match '/category/:slug', :to => 'category#index', :as => "category_jobs"
#category = Category.get_by_slug(params[:slug])
#jobs = Job.where("category_id = ? AND expires_at > ?", #category.id, DateTime.now)
.paginate(:page => params[:page], :per_page => Jobeet::Application::MAX_JOBS_ON_CATEGORY_PAGE)
.order("expires_at desc")
When I trying get category.id in controller I am getting error:
undefined method `id' for #
Could somebody give me any advice?
If you expect a single record you should do:
#category = Category.get_by_slug(params[:slug]).first
because .where(something) doesn't return a single record.
I'd like to implement a simple notification system on my site where an unseen/unread item is displayed to the user. Similar to the one used across Stack Exchange for the user's inbox where unread comments on questions, etc are displayed.
I came across this question that provides an overview of how I'd do this. What I'm confused about is how to figure out if something has been read. I could add a read_at column but how do I actually fill it? If anyone could help me with some basic guidance I'd appreciate it!
UPDATE #1: What if I add a conditional to my Item#show action where I check the user_id (ID of the user creating the item) against current_user.id. Something like the below:
unless #item.user_id == current_user.id
#item.read_at = Time.now
end
UPDATE #2: Using the code below, I'm attempting to update the message's read_at if its recipient_id matches the current_user ID. However it's not working.
def show
#message = Message.find(params[:id])
if #message.recipient_id == current_user.id
#message.read_at == Time.now
#message.save
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #message }
end
end
FINAL UPDATE: Thanks to #prasvin, here's my solution. I added a read_at column to the object. The object also has an existing recipient_id column. So in my Controller's show action, I put the following:
def show
#message = Message.find(params[:id])
if #message.recipient_id == current_user.id
#message.update_attributes(:read_at => Time.now)
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #message }
end
end
Then in my helper.rb file:
def new_messages
Message.where(:recipient_id => current_user.id, :read_at => nil).count
end
And in my layout:
<% if new_messages > 0 %><span class="notification"><%= new_messages %></span><% end %>
How about filling in read_at column in show action, i.e. we have the object in the show action,and then update its read_at attribute before redering the page
I didn't figure out the mechanics of the routing yet..
I have a User, Msg and comment model.
A User creates a Msg and inside the msg i'd like to put simple text box for comments. similar to twitter.
However, When the the form is submitted (using the form below) instead of returning to localhost/msgs/:id it returns to localhost/comments.
I have no view for /comments and I don't want to have. I want all comments to be displayed in msg/:id page.
comments controller:
class CommentsController < ApplicationController
before_filter :authenticate
def create
msgid = flash[:msg]
#current_msg = Msg.find(discid)
#comm = #current_msg.comments.build(params[:comment])
#comm.user_id = current_user.id
#comm.msg_id = msgid
puts discid
if #comm.save
flash[:success] = "Comment posted"
redirect_to msg_path(discid)
else
flash[:error] = "Comment was not posted."
redirect_to msg_path(discid)
end
end
route.rb
match '/comments' , :to => 'msgs#show'
resources :users
resources :msgs
since the comments are displayed in the show view of the msgs here is the show action in the msgs controller
def show
#msg= Msg.find(params[:id])
#title = #msg.title
#comment = Comment.new
#comments = #msg.comments.all
flash[:msg] = #msg.id
end
The error I get is
ActiveRecord::RecordNotFound in MsgsController#show
Couldn't find Msgwithout an ID
and it points to line 46 which at the moment is #msg = Msg.find(params[:id])
If I remove the route line and put a regular resources :comments I get a missing template for comments/create..
Help is appreciated.
Issue solved.. I add a :id => discid in the redirecא_to..
Also used Template is missing to understand the issue
I'm using the rails gem kaminari (https://github.com/amatsuda/kaminari) in order to paginate my posts database. Currently I have the code #posts = Post.order('id').page(params[:page]).per(5) in my controller, but this orders the pages from earliest to most recent. How do I reverse this and order from most recent to earliest?
in your model you can do:
default_scope order("created_at DESC")
or
default_scope order("created_at ASC")
def index
#all = Model.all
#all = Model.order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
def sort_column
Model.column_names.include?(params[:sort]) ? params[:sort] : "updated_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "DESC/ASC"
end