How can I set a helper method in my app that when I call it, recognizes a current_patient from a certain medic (can be multiple medics, and multiple patients for a medic) and access to patient :id, this would help me to associate:
Medic with a Patient with a Consultations for current_patient
I need to access on a patient and set id (patient) on a Consultation table foreign key :patient_id
in my create action in the controller I have:
def create
#consultation = Consultation.new(consultation_params.merge({:patient_id => current_patient}))
respond_to ...
end
Is a helper method a good way to do this?
How can I do this?
my models:
class Medic < ActiveRecord::Base
has_many :patients
end
class Patient < ActiveRecord::Base
belongs_to :medic, :foreign_key => :medic_id
has_many :consultations
accepts_nested_attributes_for :consultations
end
class Consultation < ActiveRecord::Base
belongs_to :patient, :foreign_key => :patient_id
end
Thanks for help
Lets start with the routes:
resources :patients do
resources :consultations
end
This will give us the routes for consultations nested under the patient:
Prefix Verb URI Pattern Controller#Action
patient_consultations GET /patients/:patient_id/consultations(.:format) consultations#index
POST /patients/:patient_id/consultations(.:format) consultations#create
new_patient_consultation GET /patients/:patient_id/consultations/new(.:format) consultations#new
edit_patient_consultation GET /patients/:patient_id/consultations/:id/edit(.:format) consultations#edit
patient_consultation GET /patients/:patient_id/consultations/:id(.:format) consultations#show
PATCH /patients/:patient_id/consultations/:id(.:format) consultations#update
PUT /patients/:patient_id/consultations/:id(.:format) consultations#update
DELETE /patients/:patient_id/consultations/:id(.:format) consultations#destroy
So lets say we have a PatientsController#index method which shows all the patients. And in our view we have something like this:
<% #patients.each do |patient| %>
<li>
<p><%= patient.name %></p>
</li>
<% end %>
So lets add a link to create the consultation:
<% #patients.each do |patient| %>
<p><%= patient.name %></p>
<ul class="actions">
<li>
<%= link_to "New consultation",
new_patient_consultation_path(patient) %>
</li>
</ul>
<% end %>
Clicking the link would take us to /patients/6/consultations/new.
So in our ConsultationsController we can access the patient id from the params:
class ConsultationsController < ApplicationController
# We use a callback so that we don't need to do
# #patient = Patient.find(params[:id]) in every action
before_action :set_patient
# GET /patients/:patient_id/consultations/new
def new
#consultation = #patient.consultations.new
end
# POST /patients/:patient_id/consultations
def create
#consultation = #patient.consultations.new(consultation_params)
# ...
end
# ...
private
def set_patient
#patient = Patient.find(params[:patient_id])
end
# ...
end
set_patient is just a private method belonging to the controller. This is not really a case where you would use a helper method.
Its often done when dealing with dealing with authentication since you are getting current_user from the session - independently from the params. If you are creating a helper method it should work everywhere.
There is one final thing you need to to get this to work, the form needs to point to /patients/:patient_id/consultations.
# app/views/consultations/_form.html.erb
<% form_for [#patient, #consultation] do |f| %>
# .. rails does all the magic figuring out the url.
<% end %>
http://guides.rubyonrails.org/routing.html#nested-resources
Related
I have a model that works like this:
class User < ApplicationRecord
has_many :deals
...
class Deal < ApplicationRecord
belongs_to :user
has_many :clients
...
class Clients < ApplicationRecord
belongs_to :deal
If I want to find all the clients listed on a certain deal I can enter Client.find_by(deal_id: x) in the console where x is the deal ID I want, but when I try to list them all on a page I'm doing something wrong.
Here's the relevant resource route
Rails.application.routes.draw do
resources :deals, only: [:show]
end
I think the problem is I'm not doing the find correctly in the controller
class DealsController < ApplicationController
def show
#deal = Deal.find_by_id(params[:id])
#client = Client.find_by(deal_id: params[:id])
end
end
On the page I'm trying to list clients like this:
<div class="client">
<% #client.each do |c| %>
<%= c.name %>
<% end %>
</div>
But the error is undefined method each' for #<Client:0x007f8e4cdecfb0>
I thought it would be pretty simple because the :id that's returned from /deal/:id is shared by both, but I'm stumped.
find_by will return only one object, each is a method defined for arrays.
So if you want clients for a particular deal, you can do
#deal = Deal.find_by_id(params[:id])
#clients = #deal.clients
or
#deal = Deal.find_by_id(params[:id])
#clients = Client.where(deal_id: params[:id])
and in view
<% #clients.each do |c| %>
<%= c.name %>
<% end %>
I current have my project set up like this:
resources :boards, :path => '' do
resources :posts, :path => 'thread' do
resources :replies
On /board1/ only posts from board1 show, same for board2. In /board1/thread/1/ it shows post 1 and the replies to it.
However in /board2/thread/1/ the post that is showing is from board1/thread/1/, and in the reverse board1/thread/2/ shows the post from board2/thread/2/.
Each post has a related board_id in the db, and each reply has the related post_id in the db.
How can I keep these separate?
class Board < ActiveRecord::Base
has_many :posts
has_many :replies, through: :posts
include FriendlyId
friendly_id :name, use: :slugged
accepts_nested_attributes_for :posts, :replies
end
class Post < ActiveRecord::Base
belongs_to :board
has_many :replies, dependent: :destroy
accepts_nested_attributes_for :replies
include FriendlyId
friendly_id :pid, use: :slugged
after_create :set_pid
def set_pid
post_max = self.board.posts.maximum(:pid)
reply_max = self.board.replies.maximum(:pid)
if post_max.to_i < reply_max.to_i
self.update_attributes(:pid => reply_max.to_i + 1)
else
self.update_attributes(:pid => post_max.to_i + 1)
end
end
end
Code to display post in /:board_id/show:
<% #board.posts.find_each do |post| %>
<%= post.subject %>
<%= post.name %>
<%= post.email %>
<%= post.created_at %>
No.<%= post.pid %>
<%= link_to "[reply]", board_posts_path(#board, #post)%>
<br>
<%= post.comment %><br><br>
<%= render "replies/replies" %>
<% end %>
Code to display post in /:board_id/thread/:id:
<p>
<%= #post.subject %>
<%= #post.name %>
<%= #post.email %>
<%= #post.created_at %>
No.<%= #post.pid %>
<br>
<%= #post.comment %>
</p>
Edit:
class RepliesController < ApplicationController
def create
#board = Board.friendly.find(params[:board_id])
#post = Post.friendly.find(params[:post_id])
#reply = #post.replies.create(reply_params)
redirect_to #board
end
private
def reply_params
params.require(:reply).permit(:name, :email, :subject, :comment, :pid)
end
end
class PostsController < ApplicationController
def show
#boards = Board.all
#replies = Reply.all
#post = Post.friendly.find(params[:id])
end
def create
#board = Board.friendly.find(params[:board_id])
#post = #board.posts.create(post_params)
if #post.save
redirect_to #board
else render #board
end
end
private
def post_params
params.require(:post).permit(:name, :email, :subject, :comment, :pid)
end
end
The missing part here is the RepliesController which is the source of the problem if I got the question correctly.
Most probably you have there something like #replies = current_post.replies which fetch all replies of the given post regardless of the current board. Scoping post by board will solve the problem:
current_post = Post.find_by(board_id: params[:board_id], id: params[:post_id])
if current_post
#replies = current_post.replies
end
On your friendly_id declaration in the Post model, you don't have the pid as globally unique. Use this form of friendly_id, instead:
friendly_id :pid, use: :scoped, scope: :board
In this way, duplicate friendly_id values for pid are kept separate by the board that they belong to. This is necessary for slugging nested resources properly. The :scoped value says that it's for nested (scoped) models, and the scope: key indicates that posts is nested within boards. Note that you may have to do this with replies, as well.
You'll also want to make sure that your indexes for your :slug are correct. Typically when the :scope is incorrect, you'll find it when you try to save the record. In this case, it looks like the indexes might not be set correctly to ensure the uniqueness of the board name/post pid combination. Check out Friendly ID 4 Using scoped module for more information.
When you have the indexes sorted out, you'll find that inserting new records will require you to have the friendly_id (based on your pid) already assigned. You may also want to look into using slug candidates to dynamically generate the proper slug at creation time. Also check out slug candidates rails 4 for some usage information.
I have the following association setup
class Donation < ActiveRecord::Base
belongs_to :campaign
end
class Campaign < ActiveRecord::Base
has_many :donations
end
in my donation controller i have campaign_id as a strong parameter. Now when creating a donation i would like to have the campaign_id available to save in that form, but better off in the controller somewhere im guessing as less open to being edited. To get from the campaign to the donation form i click a button
<%= link_to 'Donate', new_donation_path, class: 'big-button green' %>
as we are already using
def show
#campaign = Campaign.find(params[:id])
end
How can i get that id into my donations form?
Thanks
It sounds like you're looking for Nested Resources.
Basically, your routes.rb might look something like:
resources :campaigns do
resources :donations
end
Your controller might look something like:
class DonationsController < ApplicationController
before_action :find_campaign
def new
#donation = #campaign.donations.build
end
def create
#donation = #campaign.donations.create(params[:donation])
# ...
end
protected
def find_campaign
#campaign ||= Campaign.find(params[:campaign_id])
end
end
And your example link, something like...
<%= link_to 'Donate', new_campaign_donation_path(#campaign), class: 'big-button green' %>
I have an app in which users can follow law firms
I have 3 models
- User
- Firm
- Follow
class Firm < ActiveRecord::Base
has_many :follows, :dependent => :destroy
has_many :users, :through => :follows
class User < ActiveRecord::Base
has_many :follows, :dependent => :destroy
has_many :firms, :through => :follows
class Follow < ActiveRecord::Base
belongs_to :firm
belongs_to :user
In a table in my firms index view, I would like to take the current signed and create an association between that user and the law firm - through the follow table.
In effect doing this -
firm.users << User(current)
This is the code that I have at present, how would you suggest that I structure the path, and the corresponding controller?
<% #firms.each do |firm| %>
<tr id = "firm_<%= firm.id %>">
<td><%= link_to image_tag(firm.logo_url, :size => "80x120"), firm.url %></td>
<td><%= link_to firm.name, firm_path(firm) %></td>
<% if user_signed_in? %><td>
<%= button_to 'Follow', ? , method: :post %>
</td>
<% end %>
I am using devise for the User authentication and have put the following helpers into application helper to allow my login partial to function in a different models view.
def resource_name
:user
end
def resource_id
:user_id
end
def resource
#resource ||= User.new
end
The simplest way would be to have a follow action on a FirmsController.
In config/routes.rb:
resources :firms do
post :follow, on: :member
end
In your FirmsController:
def follow
#firm.users << current_user
end
In your view:
<%= link_to "Follow", follow_firm_path(#firm), method: :post %>
Another way would be to represent a follow relationship as a singular resource. You'd follow a firm by POSTing to /firms/1234/follow and you'd unfollow a firm by sending a DELETE request to /firms/1234/follow.
If you wanted to take that approach, you'd stick this in your config/routes.rb:
resources :firms do
resource :follow, on: :member
end
And you'd create a FollowsController like this:
class FollowsController < ApplicationController
def create
#firm = Firm.find params[:firm_id]
#firm.users << current_user
# respond with success
end
def destroy
#firm = Firm.find params[:firm_id]
#firm.users.delete current_user
# respond with success
end
end
I have the following models:
class User < ActiveRecord::Base
has_many :subscriptions
end
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :queue
end
class Queue < ActiveRecord::Base
has_many :subscriptions
end
I want to have some meta-data in the Subscription class and allow users to maintain the details of each of their subscriptions with each subscriptions meta-data. Queues produce messages, and these will be sent to users who have Subscriptions to the Queue.
As I see it the resource I want to have is a list of subscriptions, ie the user will fill in a form that has all the Queues they can subscribe to and set some metadata for each one. How can I create a RESTful Rails resource to achieve this? Have I designed my Subscription class wrong?
I presently have this in my routes.rb:
map.resources :users do |user|
user.resources :subscriptions
end
But this makes each subscription a resource and not the list of subscriptions a single resource.
Thanks.
This can be done quite easily using accepts_nested_attributes_for and fields_for:
First in the User model you do the following:
class User < ActiveRecord::Base
has_many :subscriptions
accepts_nested_attributes_for :subscriptions, :reject_if => proc { |attributes| attributes['queue_id'].to_i.zero? }
# if you hit scaling issues, optimized the following two methods
# at the moment this code is suffering from the N+1 problem
def subscription_for(queue)
subscriptions.find_or_initialize_by_queue_id queue.id
end
def subscribed_to?(queue)
subscriptions.find_by_queue_id queue.id
end
end
That will allow you to create and update child records using the subscriptions_attributes setter. For more details on the possibilities see accepts_nested_attributes_for
Now you need to set up the routes and controller to do the following:
map.resources :users do |user|
user.resource :subscriptions # notice the singular resource
end
class SubscriptionsController < ActionController::Base
def edit
#user = User.find params[:user_id]
end
def update
#user = User.find params[:user_id]
if #user.update_attributes(params[:user])
flash[:notice] = "updated subscriptions"
redirect_to account_path
else
render :action => "edit"
end
end
end
So far this is bog standard, the magic happens in the views and how you set up the params:
app/views/subscriptions/edit.html.erb
<% form_for #user, :url => user_subscription_path(#user), :method => :put do |f| %>
<% for queue in #queues %>
<% f.fields_for "subscriptions[]", #user.subscription_for(queue) do |sf| %>
<div>
<%= sf.check_box :queue_id, :value => queue.id, :checked => #user.subscribed_to?(queue) %>
<%= queue.name %>
<%= sf.text_field :random_other_data %>
</div>
<% end %>
<% end %>
<% end %>
I found this tutorial very useful, as I was trying to relate Users to Users via a Follows join table: http://railstutorial.org/chapters/following-users