I have models:
Group(has_many students).
Student(belongs_to groups).
That's code of routes.rb
root 'courses#index'
resources :students, :teachers, :groups, :courses, :users
That's code of def index and def group_params in GroupsController
def index
#groups = Group.all
end
private
def group_params
params.require(:group).permit(:name, :course_id)
end
That's code of groups/index.html.erb
<% #groups.each do |g| %>
<tr>
<td><%=link_to g.id, group_path(g)%></td>
<td><%=link_to g.name, group_path(g)%></td>
<td><%=link_to g.course.name, course_path(g)%></td>
**<td><%=link_to g.students.name, student_path(g)%></td>**
<% end %>
I wrote two students in one group. How I can view names of all students of one group in index view? I try change to
g.student.name,
students_path(g),
<%students.each do%>
<td><%=link_to g.students.name, student_path(g)%></td>
<% end %>,
resources :students do
resources :groups
end,
resources :groups do
resources :students
end
No one not working.
Please help with this problem.
routes.rb
resources :groups do
resources :students
end
groups/index.html.erb
<% #groups.each do |g| %>
<tr>
<td><%=link_to g.id, group_path(g)%></td>
<td><%=link_to g.name, group_path(g)%></td>
<td><%=link_to g.course.name, course_path(g)%></td>
<% g.students.each do |student| %>
<td><%=link_to student.name, student_path(g)%></td>
<% end %>
</tr>
<% end %>
Related
I have the following code:
<%= link_to new_book_path(controller: :books, action: 'new', id: comment) %>
#also tried:
<%= link_to new_book_path(comment.user.id) %>
#outputs: undefined id
<%= link_to new_book_path(comment.user_id) %>
#leads to my (logged-in user) book list, not this user's
<%= link_to new_book_path(comment.user) %>
#same
<%= link_to new_book_path(comment) do %>
#same. comment.post.book.user.id also same.
I was wondering how I can get to this particular user's book list through link_to from this user's comment. I keep going to my own.
My routes are:
resources :books do
resources :posts, shallow: true
end
resources :posts do
resources :comments, shallow: true
end
resources :users do
resources :comments, shallow: true
end
1) in comment:
<%= link_to comment.user.name, books_list_path(user_id: comment.user.id)
books_list_path is path where books are listed
2) in action where books are listed (index or new action):
class BooksController < ApplicationController
..
def index
#books = Book.where(user: params[:user_id])
end
..
end
3) in books/index.html.erb
<% #books.each do |book| %>
<%= book.name %><br>
<% end %>
I am having a problem in the eyes when presenting the data.
I have 3 user models, sponsors and pets Result that sponsors in a table join between users and pets that are nan, my problem is in the hour to show all the sponsors of the mascot in sight and achievement but in a wrong way to La time to msotrar the data. I hope you can tell me how to fix it. Thank you.
Index.html.erb
<h1>Sponsors#index</h1>
<table class="table table-bordered table-hover table-striped" id="histories">
<thead>
<tr>
<th>Mascota</th>
<th>Padrinos</th>
<th>Apadrinar</th>
</tr>
</thead>
<tbody>
<% #pets.each do |pet| %>
<tr>
<td><%= pet.name %></td>
<td>
<% #users.each do |user| %>
<% #sponsors.each do |sponsor| %>
<% if user.id == sponsor.user_id and pet.id == sponsor.pet_id %>
<%= user.email%>
<% else %>
<p>No Tengo Padinos =-( </p>
<% end %>
<% end %>
<% end %>
</td>
<td><button>Apadrinar</button></td>
</tr>
<% end %>
</tbody>
</table>
My controller has the three models that I am sending to view.
def index
#sponsors = Sponsor.all
#users = User.all
#pets = Pet.all
end
pet.rb
class Pet < ApplicationRecord
has_many :adoptions
belongs_to :race, required: false
has_many :users, through: :sponsors
end
user.rb
class User < ApplicationRecord
has_many :pets, through: :sponsors
end
sponsor.rb
class Sponsor < ApplicationRecord
belongs_to :user
belongs_to :pet
end
The output that now shows me the attachment in the image.enter image description here
I also wonder if there is a better way to make the query, to show the respective data.
Another thing is how would I do to no longer be able to sponsor pets that I have already given a sponsor?
You are using a through table to join users and pets, but your associations aren't setup properly. For a through model, you have to have_many of the through table along with a has_many: :through association. Your associations should look like this:
class Pet < ApplicationRecord
has_many :adoptions
belongs_to :race, required: false
**has_many :sponsors**
has_many :users, through: :sponsors
end
class User < ApplicationRecord
**has_many :sponsors**
has_many :pets, through: :sponsors
end
Afther working and following diferent recomendations here is my solution.
<% #pets.each do |pet| %>
<tr>
<td> <%= pet.name %></td>
<td>
<% if pet.sponsors.any? %>
<% pet.sponsors.each do |sponsor| %>
| <%= sponsor.user_email %> |
<% end %>
<% else %>
<p>No Tengo Padinos =-( </p>
<% end %>
</td>
<td>
<%= link_to "Apadrinar", {:controller => "sponsors", :action => "new", :mascot => pet.id }%>
</td>
</tr>
<% end %>
I changed also my models.
sponsor.rb
class Sponsor < ApplicationRecord
belongs_to :user
belongs_to :pet
has_many :gifts
delegate :email, to: :user, prefix: true, allow_nil: true
end
user.rb
class User < ApplicationRecord
has_many :sponsors
has_many :pets, through: :sponsors
end
pet.rb
class Pet < ApplicationRecord
has_many :sponsors
has_many :users, through: :sponsors
end
And finally mi index on Controller.
sponsors_controller.rb
def index
# #sponsors = Sponsor.all
# #users = User.all
# #pets = Pet.all
# #pets_no_sponsors = Pet.where.not(id: Sponsor.select("pet_id")).select("id")
#pets = Pet.includes(:sponsors)
end
By making a delegation now I only make a query which sends everything necessary to my index.
Ok, so I know this isn't preferred method, but my client needs fixed urls that can be indexed by Google and dynamic urls won't cut it. Everything works great, all my product resources work as they should, except on the 4th and final nest, I can't get a list of my objects on the subcategories/show route; I am sure I am missing something simple.
routes
resources :locations do
resources :categories do
resources :subcategories do
resources :products
end
end
end
subcategories/show view
I need to list the products that belong to the subcategory
...
<h1><%= #subcategory.name %></h1>
<%= sanitize #subcategory.body %>
...
<% #location...#category...#subcategory.products.each do |p| %>
<tr>
<td><%= p.name %></td>
<td><%= p.slug %></td>
<td><%= link_to 'Show', location_category_subcategory_product_path(#location, #category, #subcategory, p) %></td>
</tr>
<% end %>
...
subcategory model
has_many :products
products model
belongs_to :subcategory
products_controller
def index
#location = Location.friendly.find(params[:location_id])
#category = Category.friendly.find(params[:category_id])
#subcategory = Subcategory.friendly.find(params[:subcategory_id])
#products = Product.all
end
def show
#location = Location.friendly.find(params[:location_id])
#category = Category.friendly.find(params[:category_id])
#subcategory = Subcategory.friendly.find(params[:subcategory_id])
#product = Product.friendly.find(params[:id])
end
i'm a beginner in ruby on rails and getting an error i seem not to understand.
under the profile page for the Tradesman, i am trying to call all trades and all listed jobs under the trade. Trade has_many jobs & jobs has_many trades.
TrademanprofilesController:
class TrademanprofilesController < ApplicationController
def show
#user = current_user
end
def jobleads
end
def purchased
end
def memberBenifits
end
def account
end
def editjoblead
#trades = Trade.order(:name)
#jobs = Job.all
if params[:search].present?
#applications = Application.near(params[:search], 100, order: 'distance')
#hash = Gmaps4rails.build_markers(#applications) do |application, marker|
marker.lat application.latitude
marker.lng application.longitude
marker.infowindow application.location
marker.infowindow application.trade.name
end
else
#applications = Application.all
end
#applications_trades = #applications.group_by { |t| t.trade_id } end end
The views page which is a partial being called elsewhere. _trademanprofile_skills.html.erb:
<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #trades.each do |trade| %>
<tr>
<td>
<%= trade.name %></br>
<ul><% #trade.jobs.each do |job| %></ul>
<li><%= job.name %></li>
<% end %>
</td>
<td><%= link_to 'Show', trade %></td>
</tr>
<% end %>
</tbody>
</table>
model: Trade.rb
class Trade < ActiveRecord::Base
attr_accessible :name, :job_ids
has_many :applications
has_many :jobs, through: :applications
end
model: Job.rb
class Job < ActiveRecord::Base
attr_accessible :name, :trade_ids
has_many :applications
has_many :trades, through: :applications
end
The error i get:
NoMethodError in Trademanprofiles#editjoblead
undefined method `jobs' for nil:NilClass
and highlights this line:
<ul><% #trade.jobs.each do |job| %></ul>
<li><%= job.name %></li>
<% end %>
i tried the below but did not work
<% #trades.each do |trade| %>
<%= trade.name %>
<%= trade.jobs %>
<% end %>
i got this displayed:
#<Job::ActiveRecord_Associations_CollectionProxy:0x00000106b0f1d8>
i also tried the below but did not work
<% #trades.each do |trade| %>
<%= trade.name %>
<%= trade.job_ids %>
<% end %>
but it only returned the id numbers of the jobs under the trade Aerial / Network
Aerial / Network Specialist
[978, 979, 980, 1039, 1040, 1041]
my routes file:
Rails.application.routes.draw do
resources :images
devise_for :users
resources :jobstartdates
resources :budgets
resources :applications do
collection do
get :search
get :search_result
get :business_details
end
end
get 'home/index'
resources :trades
resources :jobs
root to: 'home#index', as: 'home'
resource :trademanprofiles do
get :show
get :jobleads
get :purchased
get :memberBenifits
get :account
get :editjoblead
end
resources :employeenumbers
resources :businesstypes
resources :trademanroles
resources :titles
resources :adverts
resources :distances
i would like the Trade name & the Job names (not the ids) to be displayed. Many thanks an i truly apologise if this is a simple question.
If you look at your method
def editjoblead
#trades = Trade.order(:name) #this will return a collection of trades to you
#jobs = Job.all
#other logic
end
and in your view you have this
<ul>
<% #trade.jobs.each do |job| %></ul>
<li><%= job.name %></li>
<% end %>
First of all you need to correct your html you have closed ul and then using li and secondly you are trying to get a collection on a collection(#trade is a collection and jobs will give you a collection)
You need to do this:
<% #trades.each do |trade| %>
// this will give a single trade present in #trades
<tr>
<td>
<%= trade.name %></br>
<ul>
<% trade.jobs.each do |job| %>
// this will give a job associated with your trade, notice it's trade and not #trade
<li><%= job.name %></li>
<% end %>
</ul>
</td>
<td><%= link_to 'Show', trade %></td>
</tr>
<% end %>
what about your routes.rb file? I thing the error may be in there put like this in your route file
resources :trades do
resources :applications
resources :jobs
end
EDIT: I've altered the code to now read:
View code:
<h3>Your Orders:</h3>
<table class="order-items">
<th>Date</th>
<th>Order No.</th>
<th>View Order</th>
<% #orders.each do |order| %>
<tr>
<td><%= order.created_at.strftime("%d %b. %Y") %></td>
<td><%= order.id %></td>
<td><%= link_to 'Show', order_path(order) %></td>
</tr>
<% end %>
</table>
<p><%= will_paginate #orders %></p>
Controller Code:
#customer = Customer.find(params[:id])
#orders = #customer.orders.paginate page: params[:page], order: 'id desc',
per_page: 5
Routes:
resources :orders
resources :customers
get 'admin' => 'admin#index'
get 'account' => 'customers#show'
match '/account', :to => 'customers#show'
match'/signup', :to => 'customers#new'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
resources :users
resources :line_items
resources :carts
get "store/index"
resources :products do
get :who_bought, on: :member
end
resources :line_items do
put 'decrease', on: :member
put 'increase', on: :member
end
root to: 'store#index', as: 'store'
The problem is now that I'm getting the error:
The #orders variable appears to be empty. Did you forget to pass the collection object for will_paginate?
whenever I click on a "Show" link in the list of orders in the view. Tried restarting the server just in case, but can't get it working :S!
It should be
<% #orders.each do |order| %>
not
<% #customer.orders.each do |order| %>
Pls check this code
#customer = Customer.find(params[:id])
#orders = #customer.orders.paginate page: params[:page], order: 'title',per_page: 5
in view
<% #orders.each do |order| %>