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
Related
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 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 %>
I'm new to Rails and I'm building a quizz application.
I have a has_many and belongs_to association set up between two models: Level and Question.
#models/level.rb
class Level < ActiveRecord::Base
has_many :questions
end
#models/question.rb
class Question < ActiveRecord::Base
belongs_to :level
attr_accessible :level_id
end
I have an action 'startlevel' in my LevelController that simply lists all the levels.
class LevelsController < ApplicationController
def startlevel
#levels = Level.all
end
and the view with links to go to the first question of the level. I want to add the id of the level as a parameter in the link. I noticed the 1 in the url of the view. I have no idea why how it came there and if it is part of my problem.
#controller/levels/startlevel/1
<h2>which level would you like to play</h2>
<table>
<tr>
<th>level</th>
</tr>
<% #levels.each do |level| %>
<tr>
<td> level <%=level.number %></td>
<td><%= link_to '<> play this level', :controller => "questions", :action => "answer",:level_id=> level.id%></td>
</tr>
<% end %>
</table>
When I follow the the link, I want to go to the first question with a level_id that matches the id parameter in the link_to, so i tried to do this:
class QuestionsController < ApplicationController
def answer
#question = Question.find_by_level_id(params[:level_id])
end
with this view
<p>
<b>Question:</b>
<%=h #question.word %>
</p>
<p>
<b>1:</b>
<%=h #question.ans1 %>
</p>
<p>
<b>2:</b>
<%=h #question.ans2 %>
</p>
<p>
<b>3:</b>
<%=h #question.ans3 %>
</p>
<p>
<b>4:</b>
<%=h #question.ans4 %>
</p>
<%= form_tag(:action => "check", :id => #question.id) do %>
<p>
<b>the correct answer is number: </b>
<%=text_field :ans, params[:ans]%>
</p>
<p><%= submit_tag("check")%></P>
<% end %>
unfortunately, whatever i tried, all i got for the last couple of hours was:
undefined method `word' for nil:NilClass
(word is an attribute of a question)
I want to cry. what am I doing wrong?
p.s. my idea is to add a link_to_unless in the 'answer'view which goes to the next question of the same level unless the next one is nil, so i think i need to group the questions with the same reference key somehow?
It works right now, however i'm not sure if this is the prettiest solution. Views/levels/play is empty since it only redirects to the first question of the level.
class LevelsController < ApplicationController
def startlevel
#levels = Level.all
end
def play
#level = Level.find(params[:id])
#question = Question.find_by_level_id(params[:id])
redirect_to controller: 'questions', action: 'answer', id: #question.id
end
#views/levels/startlevel
<h2>Which level would you like to play</h2>
<table>
<tr>
<th>level</th>
</tr>
<% #levels.each do |level| %>
<tr>
<td> level <%=level.number %></td>
<td>
<%= link_to '<> Play this level', :action => "play", :id=>level.id %></td>
</tr>
<% end %>
</table>
QuestionsController
class QuestionsController < ApplicationController
def answer
#question= Question.find(params[:id])
end
edit: Routes:
quizz::Application.routes.draw do
resources :questions
resources :levels
get "home/index"
root :to => 'home#index'
match ':controller/:action/:id', via: [:get, :post]
match ':controller/:action/:id.:format', via: [:get, :post]
I'm using the thumbs_up gem in my application and I'm trying to figure out the best way to save votes in my controller while iterating.
Here is my model:
class Vendor < ActiveRecord::Base
has_many :inventory_items
has_many :items, through: :inventory_items
has_many :shopping_lists, through: :inventory_items
acts_as_voteable
end
my current controller:
def vote_for_vendor
# not sure what to put in here
end
def vote_against_vendor
# not sure what to put in here
end
my current view:
<% provide(:title, 'Stores') %>
<table class="table table-condensed table-hover">
<tr>
<th>Name</th>
<th>Address</th>
<th>Favorite?</th>
</tr>
<% #vendors.each do |v| %>
<tr>
<td><%= v.name %></td>
<td><%= v.address %></td>
<td>
<% if current_user.voted_for(v) %>
<%= link_to 'unlike', vote_against_vendor_vendors_path(vendor_id: v.id), :remote => true %>
<% else %>
<%= link_to 'like', vote_for_vendor_vendors_path(vendor_id: v.id), :remote => true %>
<% end %>
</td>
</tr>
</table>
Most of the examples I've seen have used params([]) to pass the relevant information to the controller. I don't really have params because this is just my index page that shows all vendors. How can I save votes using this gem while iterating? Thanks in advance!
Updated Controller w/ help from MrYoshi
class VendorsController < ApplicationController
def index
#vendors = Vendor.all
end
def vote_for_vendor
vendor = Vendor.find(params[:vendor_id])
current_user.vote_for(vendor)
respond_to do |format|
format.js
end
end
def vote_against_vendor
vendor = Vendor.find(params[:vendor_id])
current_user.vote_against(vendor)
respond_to do |format|
format.js
end
end
end
my routes:
resources :vendors do
collection { post :vote_for_vendor }
collection { post :vote_agaist_vendor }
end
Current Server Error
Started GET "/vendors/vote_for_vendor?vendor_id=4" for 127.0.0.1 at 2013-09-06 10:07:29 -0700
AbstractController::ActionNotFound (The action 'show' could not be found for VendorsController):
...........
Rendered /Users/#Myname/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/unknown_action.erb within rescues/layout (0.5ms)
I give you the start of what you want, and you will be able to do the rest by yourself I think:
View:
<% if current_user.voted_for(v) %>
<%= link_to 'unlike', vote_against_vendor_vendors_path(vendor_id: v.id), :remote => true %>
<% else %>
<%= link_to 'like', vote_for_vendor_vendors_path(vendor_id: v.id), :remote => true %>
<% end %>
Controller:
def vote_for_vendor
vendor = Vendor.find(params[:vendor_id])
current_user.vote_for(vendor)
render :nothing => true
end
And the vote_against is pretty simple to guess now that you have this one above ;)
For your 'current server error' (AbstractController::ActionNotFound (The action 'show' could not be found for VendorsController):), you need to add a member route to your config/routes.rb file, like this:
resources :vendors do
member do
post 'vote_for_vendor'
post 'vote_against_vendor'
end
end
You are currently using collection routes which are for resources that don't need a particular ID to work. I highly recommend reading the Rails Guide on routes:
Rails Guide Documentation on Routes
hi im having a bit trouble in my application im new in ROR development
i made a static page where my function rooms of my reservation
application are showed and can be add in in the
reservation_function_room(line item of reservation functionroom) it
raises uninitialized constant Reservations when ever i route to <%= link_to "add functionrooms", reservation_pages_functionroom_path(#reservation) %> cant figure out whats wrong
very thanks in advance thanks
page-static pages
class PagesController < ApplicationController
def functionroom
#reservation = Reservation.find(params[:reservation_id])
#function_room = FunctionRoom.all
end
end
functionroom.html.erb
<% if notice %>
<p id = "notice"><%= notice%></p>
<%end%>
<h1>functionRooms</h1>
<%#function_room.each do |functionroom|%>
<h3><%= functionroom.name%></h3>
<p><%= number_to_currency(functionroom.price)%></p>
<%= button_to 'add function room',
reservation_reservation_function_room_path(:function_room_id =>
functionroom), :method => :post,:remote => true%>
<%end%>
reservation_contoller.rb
def index
#reservations = Reservation.all
end
def show
#reservation = Reservation.includes(:reservation_function_rooms =>
:function_room,:reservation_package => :package).find(params[:id])
end
class ReservationFunctionRoomsController < InheritedResources::Base
def show
#reservation_function_room =
ReservationFunctionRoom.find(params[:id])
end
def new
#reservation_function_room = ReservationFunctionRoom.new
end
def create
#reservation = Reservation.find(params[:reservation_id])
function_room = FunctionRoom.find(params[:function_room_id])
#reservation_function_room =
#reservation.add_function_room(function_room.id)
if #reservation_function_room.save
redirect_to #reservation, :notice => "function room successfuly
added"
end
end
end
routes
get "pages/menu"
resources :reservation_function_rooms
resources :services
resources :reservations do
get "pages/functionroom"
end
resources :reservation_packages
resources :package_line_items
resources :packages do
resources :package_crews
end
resources :function_rooms
reservation.rb
class Reservation < ActiveRecord::Base
has_one :reservation_package
belongs_to :service
has_many :reservation_function_rooms
has_many :package_line_items
has_many :menus , :through => :package_line_items, :uniq => true
has_many :function_rooms, :through =>:reservation_function_rooms
def add_function_room(function_room_id)
current_function_room =
reservation_function_rooms.find_by_function_room_id(function_room_id)
if current_function_room
redirect_to #reservation, :notice => "function room already
added"
else
current_function_room =
reservation_function_rooms.build(:function_room => function_room_id)
current_function_room.price =
current_function_room.function_room.price
end
current_function_room
end
end
reservation/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #reservation.name %>
</p>
<%= display_package #reservation%>
<p>
<b>Address:</b>
<%= #reservation.address %>
</p>
<p>
<b>Contact:</b>
<%= #reservation.contact %>
</p>
<p>
<b>Date:</b>
<%= #reservation.date %>
</p>
<p>
<b>Timestart:</b>
<%= #reservation.timeStart %>
</p>
<p>
<b>Timeend:</b>
<%= #reservation.timeEnd %>
</p>
<p>
<b>Numguest:</b>
<%= #reservation.numGuest %>
</p>
<p>
<%= link_to "add functionrooms", reservation_pages_functionroom_path(#reservation) %>
</p>
<table>
<tr>
<th>Function room</th>
<th>Price</th>
</tr>
<% #reservation.reservation_function_rooms.each do |room|%>
<tr>
<td><%= room.function_room.name%></td>
<td><%= room.function_room.price%></td>
</tr>
<%end%>
</table>
<%= link_to 'Edit', edit_reservation_path(#reservation) %> |
<%= link_to 'Back', reservations_path %>
if anything is needed feel free to ask thanks in advance :)
I know it's been 4 years, but I ran through the same issue and I think I may have found where it comes from.
You have dependency "has_one :reservation_package"
and lower you have "resources :reservation_packages"
I think the "s" here is the problem : either do a single resource OR a has_many relation. That why he can't find it.
That's how i fixed my problem.