Updated: 22/08/2019
This is working now. Thanks for feedback and help!
Image of Benefit list: List Display <-- Needed to params permit benefit_ids
Old Version: 21/08/2019
I have set up many to many associations but can't get the data table name. I can get the ID but it won't allow me to display the other fields information.
I have looked in the controller and added to permit benefit_ids but not sure if I should add anything else to it.
exercisetypes(Index.html.erb)
--- Old code (21/08/2019)
<tbody>
<% #exercisetypes.each do |exercisetype| %>
<tr>
<td><%= exercisetype.name %> </td>
<td><%= exercisetype.benefits.name %> </td>
...
---
New code (22/08/2019)
<tbody>
<% #exercisetypes.each do |exercisetype| %>
<tr>
<td> <%= exercisetype.name %> </td>
<td>
<ul>
<% exercisetype.benefits.each do |benefit| %>
<li><%= benefit.name %></li>
<% end %>
</ul>
</td>
<% end %>
...
_form.html.erb(Gem: simple form)
<% f.association :benefits, as: :check_boxes %>
Models
exercisetype.rb
has_many :exercisetype_benefits, dependent: :destroy
has_many :benefits, :through => :exercisetype_benefits
benefit.rb
has_many :exercisetype_benefits
has_many :exercisetypes, :through => :exercisetype_benefits
exercisetype_benefit.rb
belongs_to :exercisetype
belongs_to :benefit
The table displays the model name Benefit. There are no error messages but I am not sure how to display the name of the benefit.
exercisetype.benefits is a collection:
# index.html.erb
<ul>
<% exercisetype.benefits.each do |benefit| %>
<li><%= benefit.name %></li>
<% end %>
</ul>
Benefits belongs to exercisetype model, so you need to iterate over benefits to access the attributes. Check this:
<tbody>
<% #exercisetypes.each do |exercisetype| %>
<tr>
<td><%= exercisetype.name %> </td>
<td>
<ul>
<% exercisetype.benefits.each do |benefit| %>
<li><%= benefit.name %></li>
<% end %>
</ul>
</td>
</tr>
<% end %>
</table>
Exercise has_many benefits which mean when you do exercise.benefits it will return a collection of benefits object so you need to iterate through it.
Your collection will look like
`#<ActiveRecord::Associations::CollectionProxy [#<Benefit id: 1, name: "Good Health">, #<Benefit id: 2, name: "Blood flow">]>`
So you need to iterate through it like.
<% exercisetype.benefits.each do |benefit| %>
<li><%= benefit.name %></li>
<% end %>
Related
I have #schedules Object in my shift table app
and I group it use group_by method in my controller
#schedules = Schedule.all
#days_off_list = #schedules.group_by {|t| t.staff}
Model:
class Staff
has_many :schedules
end
class Schedule
belongs_to :staff
end
I want it to display like
but I use the html tag <table>, and layout it like
Jacky
2017-4-6 day off
2017-4-7 day off
Allen
2017-4-4 day off
2017-4-6 day off
In my view:
<table>
<% #days_off_list.each do |staff, schedules| %>
<tr>
<th><%= staff.name %></th>
</tr>
<% schedules.each do |schedule| %>
<tr>
<td><%= schedule.days_off.to_date %></td>
<td> break</td>
</tr>
<% end %>
<% end %>
</table>
I know how to group the #schedules by date by if else and group by attribute use group_by method, but I don't know how to use two ways together.
does I have any solution to solve the problem by group_by method or the other function?
I try the other ways to solve the question, and it can work.
the solutions is using three each loop and one if elsif to select the true date like what i write at above.
controller:
#staffs = Staff.all
#range = Date.today.beginning_of_month..Date.today.end_of_month
#schedules = Schedule.all
helper:
def search_days_off(day)
Schedule.where(days_off: day.midnight..day.end_of_day).map {|t| t.staff.number}.sort.last
end
view:
<tbody>
<% #range.each do |days| %>
<tr>
<td><%= days %></td>
<% #staffs.each do |staff| %>
<% #schedules.each do |schedule| %>
<% if days == schedule.days_off.to_date %>
<% if staff.number == schedule.staff.number %>
<td>day off</td>
<% break %>
<% elsif schedule.staff.number.equal?(search_days_off(schedule.days_off)) %>
<td> </td>
<% end %>
<% end %>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>
This is the scenario:
so, the product did not have any relation with feedback, but the order has
and i would like to show the feedbacks as belongs to products
model FEEDBACK
has_one :order
has_one :product, through: :order
model ORDER
belongs_to :product, touch: true
belongs_to :feedback
model PRODUCT
has_many :feedbacks
has_many :orders
i'm would like to get the feedbacks of the product on a loop
for exemple
controller HOME
#total_feedbacks = Feedback.joins(:product).where('products.id = ?', #product.id).where('buyer_feedback_date is not null').count
#average_rating_from_buyers = Feedback.joins(:product).where('products.id = ?', #product.id).where('buyer_feedback_date is not null').rated(Feedback::FROM_BUYERS).average(:buyer_rating)
view HOME
<table id="posts" class="product_table">
<tbody class="page">
<tr>
<% #products.last(22).each do |product|%>
<td>
<%=link_to product_path(product.id), :class=>"product" do %>
<span class="productName">
<%=product.name %></span>
<span class="price"><%=number_to_currency(product.price, :unit=> "R$") %></span>
<% if product.vitrine.feedbacks.present? %>
<div class="productFeeback">
<div><%= #average_rating_from_buyers %>"></div>
<%=#total_feedbacks %>
</div>
<% end %>
<% end %>
</td>
<% end %>
</tr>
</tbody>
</table>
with this code i'm getting
undefined method `id' for nil:NilClass
someone have a hint to spare? thank's
On your Product model, try:
has_many :feedbacks, through: :orders
For the "Undefined method id" error, it would help if you could include a bit more of your stack trace.
Just based on the example code, I think it would have to be happening in the controller. #product.id is being referenced, but I don't see #product being set.
I am currently learning Ruby on Rails, however, I have run into a slight issue, I am unable to create a text field from which to search. I so far have a working DB and web page (albeit an ugly one), however, I am having a problem with this simple function, here is my code so far:
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<strong>Search Title:</strong>
text_field_tag(:id)
<%= #article = Article.find(params[:id]) %>
<% end %>
</table>
error
/Users/jake/Documents/internships/rails/search/app/views/articles/index.html.erb:31: syntax error, unexpected keyword_ensure, expecting end-of-input
just for the web page, any help would be strongly appreciated.
Thanks in advance!
Try looking through this code:
In your html:
<% form_tag projects_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
In your controller:
def index
#projects = Project.search(params[:search])
end
and in the model
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
Here is the link
Also as I'm doing in my project, you can create separate action in your index/welcome controller called search:
def search
end
Then configure route in your routes.rb like so(not sure exactly this will work)
get 'welcome/' => 'welcom#search', as: 'search'
And then in view create a form:
<%= form_tag search_path, method: 'get', remote: true, id: "garage-search" %>
<%= text_field_tag :search %>
<% end %>
This will send search request by AJAX to your search controller. There you can process it as needed.
Also try look here. Very clearly explained..
currently im accessing my join table directly. the reason im doing this is because i only want the staff to be able to adjust the status
im facing problem with
undefined method order_task_path' for #<#<Class:0x00000009bf62c0>:0x0000000a6d7c70>`
with parameter Parameters:
{"id"=>"1,1"}
i'm able to display the join table according to my need. the problem is when i try to display it in edit.
here is my ordertask controller
class OrdersTasksController < ApplicationController
before_action :set_status, only: [:show]
def index
#orders = Order.all
##status = OrderTask.includes(:task,:order).where(order_id: params[:id])
end
def edit
#status = OrderTask.find(params[:id])
end
def show
end
def update
respond_to do |format|
if #status.update(order_params)
format.html { redirect_to #status, notice: 'Order was successfully updated.' }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit }
format.json { render json: #status.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_status
#status = OrderTask.includes(:task).where(order_id: params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order_task,:order).permit(:id,:order_id,:status)
end
end
my show.html.erb
<p id="notice"><%= notice %></p>
<table class="table table-hover">
<tr>
<td><h4>Order Number : <%= #status.first.order.order_number %></h4>
</td>
</tr>
<tr>
<td>Task
</td>
<td>Status:
</td>
</tr>
<tr>
<% #status.each do |i| %>
<td><%= i.task.task_name %>
</td>
<td><%= i.status %>
</td>
<td><%= link_to 'Edit', edit_orders_task_path(i) %></td>
</tr>
<% end %>
</table>
<%= link_to 'Back', orders_tasks_path %>
my _form.html.erb
<%= form_for(#status) do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<table class="table table-hover">
<tr>
<td><%= f.label "List of task" %>
</td>
<td><%= f.label "Status" %>
</td>
</tr>
<tr>
<td><%= f.task_id %>
</td>
<td><div class="dropdown">
<%= f.select(:status,['In progress', 'Completed'], {}, {class: "control"})%> </div>
</td>
</tr>
<tr>
<td>
</td>
<td><%= f.submit %>
</td>
</tr>
</table>
<% end %>
and my routes
resources :orders
resources :services
resources :tasks
resources :customers
resources :staffs
resources :orders_tasks
root 'staffs#index'
and lastly my ordertask model
class OrderTask < ActiveRecord::Base
self.primary_key = [:order_id,:task_id]
self.table_name = "Orders_tasks"
belongs_to :order
belongs_to :task
end
hope u guys can give suggestion or something to help me with this. thx in advance.
EDIT
here is my route.rb
C:\Users\Idea\DHMS>rake routes
Prefix Verb URI Pattern Controller#Action
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
new_order GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PATCH /orders/:id(.:format) orders#update
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
services GET /services(.:format) services#index
POST /services(.:format) services#create
new_service GET /services/new(.:format) services#new
edit_service GET /services/:id/edit(.:format) services#edit
service GET /services/:id(.:format) services#show
PATCH /services/:id(.:format) services#update
PUT /services/:id(.:format) services#update
DELETE /services/:id(.:format) services#destroy
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PATCH /tasks/:id(.:format) tasks#update
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
customers GET /customers(.:format) customers#index
POST /customers(.:format) customers#create
new_customer GET /customers/new(.:format) customers#new
edit_customer GET /customers/:id/edit(.:format) customers#edit
customer GET /customers/:id(.:format) customers#show
PATCH /customers/:id(.:format) customers#update
PUT /customers/:id(.:format) customers#update
DELETE /customers/:id(.:format) customers#destroy
staffs GET /staffs(.:format) staffs#index
POST /staffs(.:format) staffs#create
new_staff GET /staffs/new(.:format) staffs#new
edit_staff GET /staffs/:id/edit(.:format) staffs#edit
staff GET /staffs/:id(.:format) staffs#show
PATCH /staffs/:id(.:format) staffs#update
PUT /staffs/:id(.:format) staffs#update
DELETE /staffs/:id(.:format) staffs#destroy
orders_tasks GET /orders_tasks(.:format) orders_tasks#index
POST /orders_tasks(.:format) orders_tasks#create
new_orders_task GET /orders_tasks/new(.:format) orders_tasks#new
edit_orders_task GET /orders_tasks/:id/edit(.:format) orders_tasks#edit
orders_task GET /orders_tasks/:id(.:format) orders_tasks#show
PATCH /orders_tasks/:id(.:format) orders_tasks#update
PUT /orders_tasks/:id(.:format) orders_tasks#update
DELETE /orders_tasks/:id(.:format) orders_tasks#destroy
root GET / staffs#index
According to the error message undefined method order_task_path' for #<#<Class:0x00000009bf62c0>:0x0000000a6d7c70>, the url helper method does not exist.
you can rake rake routes in the terminal to get all the routes and route helpers.
resources :orders do
resources :tasks
end
generate the following routes and helpers.
order_tasks GET /orders/:order_id/tasks(.:format) tasks#index
POST /orders/:order_id/tasks(.:format) tasks#create
new_order_task GET /orders/:order_id/tasks/new(.:format) tasks#new
edit_order_task GET /orders/:order_id/tasks/:id/edit(.:format) tasks#edit
order_task GET /orders/:order_id/tasks/:id(.:format) tasks#show
PATCH /orders/:order_id/tasks/:id(.:format) tasks#update
PUT /orders/:order_id/tasks/:id(.:format) tasks#update
DELETE /orders/:order_id/tasks/:id(.:format) tasks#destroy
resources :orders do
resources :orders_tasks
end
generate the following routes and helpers.
order_orders_tasks GET /orders/:order_id/orders_tasks(.:format) orders_tasks#index
POST /orders/:order_id/orders_tasks(.:format) orders_tasks#create
new_order_orders_task GET /orders/:order_id/orders_tasks/new(.:format) orders_tasks#new
edit_order_orders_task GET /orders/:order_id/orders_tasks/:id/edit(.:format) orders_tasks#edit
order_orders_task GET /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#show
PATCH /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#update
PUT /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#update
DELETE /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#destroy
http://guides.rubyonrails.org/routing.html#nested-resources
I have 2 models, car and registrations.
class Car < ActiveRecord::Base
belongs_to :Registration
end
class Registration < ActiveRecord::Base
has_many :cars, :dependent => :destroy
accepts_nested_attributes_for :cars, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
In CarsController:
def index
#cars = Car.all
#cars2 = Car.all(:joins => :Registration)
end
In view:
<% #cars.each do |car| %>
<tr>
<td><%= car.twitter %></td>
<td><%= car.facebook %></td>
<td>
<% #cars2.Registration.each do |h| %> #here is my problem
<%= h.email %>
<% end %>
</td>
</tr>
<% end %>
This is my statement of cars. I am trying to print for every car owner's email. The email is in table Registration (model registration). I don't know how to make a query to database, that I want to get email from table Registrations, when column registration_id in table Cars == id column of table Registrations...
So I would like to ask you, if you have a tip, how to do...
You have got your associations in capital letters. It should be in small like this
class Car < ActiveRecord::Base
belongs_to :registration
end
Also you dont have to assign 2 variables in the controller
def index
#cars = Car.includes(:registration)
end
and in the view
<% #cars.each do |car| %>
<tr>
<td><%= car.twitter %></td>
<td><%= car.facebook %></td>
<td>
<%= car.registration.email %> #here is my problem
</td>
</tr>
<% end %>
Use this instead to fetch the email:
<%= car.registration.email %>
If you want eager loading, then use the following line in your controller instead:
#cars = Car.includes(:registration)
There's no need for #cars2.