I'm trying to show only the registers (patients) from a user (medic) that creates the register
In my controller I have:
def index
# #patients = Patient.all
#medic = Medic.find(params[:id])
#patients = #medic.patients
end
I get error: Couldn't find Medic with 'id'=
In my view I have:
<% #patients.each do |patient| %>
<%= patient.name %>
<% end %>
In my models I have:
class Medic < ActiveRecord::Base
has_many :patients
end
class Patient < ActiveRecord::Base
belongs_to :medic
end
¿How can I do this?
Thanks for help!
You can not have params[:id] by default in your index method; it is a collection route not a member route. So always the value of params[:id] is nil
If you would like to index patients of a certain medic you can go to localhost:3000/patients?medic_id=x and in your controller use
if params[:medic_id]
#patients = Patient.where(medic_id: params[:medic_id]).all
else
#patients = Patient.all
end
If you use devise and you have a current_medic defined then just get patients for the current medic using
#patients = current_medic.patients
localhost:3000/patients will now display patients or logged in user.
You have to add a before_action :authenticate_medic! to your controller.
Related
i added dispute new link inside orders but rails log shows
since the dispute is associated with order in the model why still show undefined local variable or method?
undefined local variable or method `new_dispute' for
<#
someone know why?
view/_order.html.erb
<ul>
<li>Order: <b><%= order.transaction.transaction_id %></b></li>
<li>seller:<%= order.seller_name %></li>
<li><%=link_to"Create New Dispute", new_dispute %></li>
</ul>
disputer controller
class DisputesController < ApplicationController
def new
#order = current_user.cart.orders.find(params[:id])
if current_user.address.blank?
redirect_to edit_user_path
flash[:error] = 'error'
else
#dispute = Dispute.new
end
end
end
class Order < ActiveRecord::Base
has_one :dispute
end
class Dispute < ActiveRecord::Base
# attr_accessible :title, :body
belongs_to :order
'
end
Try new_dispute_path.
And read all about this stuff here :)
http://guides.rubyonrails.org/routing.html#path-and-url-helpers
I have three models...
models/resident.rb
class Resident < ActiveRecord::Base
belongs_to :hostel
has_many :leaves,dependent: :delete_all
has_one :user,dependent: :delete
end
models/user.rb
class User < ActiveRecord::Base
belongs_to :resident
end
models/leave.rb
class Leave < ActiveRecord::Base
belongs_to :resident
end
Now when I am trying to access the value of leave's attribute in views/leave/show.html.erb
I am getting this:
app/views/leaves/show.html.erb
<%= #leaves %>
out put In Browser :
#<Leave::ActiveRecord_Associations_CollectionProxy:0x007fde611850f0>
My leave controller looks like :
leaves_controller.rb
class LeavesController < ApplicationController
def new
if logged_in?
#leave=Leave.new
else
flash[:info]="Please login to mark a leave"
redirect_to root_path
end
end
def show
#leaves= current_user.resident.leaves
end
def create
#leave=current_user.resident.leaves.create(leave_params)
if #leave.save
flash[:info] = "Leave successfully marked"
redirect_to new_leave_path
else
flash[:danger] = "Something wrong Happened try again"
redirect_to root_path
end
end
private
def leave_params
params.require(:leave).permit(:start_date,:end_date,:destination)
end
end
Am I making correct leaves for resident and related user (create method)?
Is show method correct ?
and How to assess the user's leaves attribute in show.html.erb of leaves views.
A Resident has_many Leaves so current_resident.leaves returns an array of all the current_resident's leaves. You will need to loop through leaves to show individual attributes. Try
#leaves.first.attribute_name
in your view to get an idea of how the data is represented. To show all the leaves you'll need to use a loop in the view
#leaves.each do |leave|
leave.inspect
end
You are doing everything fine, and show method is fine, and the template shows exactly what is was told to show.
#leaves is a collection. You probably want to show it’s elements? This should lead to the proper solution:
<% #leaves.each do |l| %>
<%= l.inspect %>
<% end %>
I have been trying to figure out how to make it so that I can create an activity page that lists out likes by other users on your OWN individual posts. In my case, I have no friend or following system so literally anyone who signs up can like a post. I have been looking everywhere and cannot find an example of how it is done
Currently, I have defined a like method in my post controller
def like
#post = Post.find(params[:id])
#post.upvote_by current_user
redirect_to :back
end
I also have an activities controller:
class ActivitiesController < ApplicationController
def index
#activities = PublicActivity::Activity.where(trackable_type: "Post", trackable_id: current_user.post_ids, key: "post.like")
end
end
My Post model looks like this:
class Post < ActiveRecord::Base
belongs_to :user
acts_as_votable
include PublicActivity::Model
tracked only: [:create, :like], owner: Proc.new{ |controller, model| model.user }
default_scope -> { order('created_at DESC') }
end
My activities view template looks like this:
<% #activities.each do |activity| %>
<%= activity.inspect %>
<% end %>
As of right now, my notification page displays nothing. How do i go about displaying a feed showing all the likes that my posts received from other users. Thanks so much
When upvote_by is called on a post, an ActsAsVotable::Vote record is created. You therefore have to add public activity to this model if you want to log votes.
class ActsAsVotable::Vote
include PublicActivity::Model
tracked only: [:create], owner: Proc.new{ |controller, model| model.user }
end
I have content_date row in my table that uses the datetime field type. I would like to order my data (content_items) using this field.
models/content_item.rb
class ContentItem < ActiveRecord::Base
belongs_to :calendar
end
models/calendar.rb
class Calendar < ActiveRecord::Base
has_many :content_items
end
controllers/content_items_controller.rb
def index
#calendar = Calendar.find(params[:calendar_id])
end
I tried this, but had no success:
controllers/content_items_controller.rb
def index
#calendar = Calendar.find(params[:calendar_id])
#content_item = #calendar.content_items.order(content_date: :desc)
end
Github Link: https://github.com/JeremyEnglert/baked
This should fix the issue:
#content_item = #calendar.content_items.order('content_date desc')
UPDATE:
I checked your Github repo. You are not using the instance variable in the controller to display the content items. You have to change:
app/views/content_items/index.html.erb
<% #calendar.content_items.each do |content_item| %>
to:
<% #content_item.each do |content_item| %>
#content_items = Calendar.includes(:content_items).order('content_items.content_date desc')
This will solve your problem.
I am working on a rails 4 application that currently has two models User and Status. In the user model I defined the association below. Both the status and user tables are populating with information. Statuses are loading with an associated user_id
User Model
class Status < ActiveRecord::Base
belongs_to :user
end
I have the following block in my show status view which will display the user_id and and the content of the status
<% #statuses.each do |status| %>
<div class="status">
<strong> <%=status.user_id%></strong>
<p> <%=status.content%></p>
I would like to display the user's first name instead. According the tutorial i'm taking I should be able to use this code since I have the association defined however it's returning the error below.
<%=#status.user.first_name%>
Error
#==>undefined method `first_name' for nil:NilClass
How can I display first_name in the controller? Do I need to define a new method for user or should the association provide?
Relevant Controller Code for Reference
class StatusesController < ApplicationController
before_action :set_status,:set_user, only: [:show, :edit, :update, :destroy]
# GET /statuses
# GET /statuses.json
def index
#statuses = Status.all
end
# GET /statuses/1
# GET /statuses/1.json
def show
puts "debug msg #{#status.inspect}"
end
# GET /statuses/new
def new
#status = Status.new
end
# GET /statuses/1/edit
def edit
end
# POST /statuses
# POST /statuses.json
...
...
...
private
# Use callbacks to share common setup or constraints between actions.
def set_status
#status = Status.find(params[:id])
puts "in set status"
end
def set_user
#status.user = User.find_by(#status.user_id)
end
# Never trust parameters from the scary internet, only allow the white list through.
def status_params
params.require(:status).permit(:content, :user_id)
end
end
Sees like there is no problem in your code. The error undefined method first_name for nil:NilClass means that the status object not associated with user or user have no field first_name. Try following code:
<% #statuses.each do |status| %>
<div class="status">
<strong> <%=status.user.try(:first_name) %></strong>
<p> <%=status.content%></p>
I am not sure what page you are trying to display <%=#status.user.first_name%> this on, but this should work.
You can use the will_paginate gem:
def show
#statuses = #statuses.paginate(page: params[:page])
end
add this to the view:
<%= will_paginate %>
or this should be the normal way:
def show
#statuses = #statuses.find(params[:id])
end