No Method Error in Japas#index - ruby-on-rails

I generated a scaffold called rounds. Here is what my JapasController looks like:
class JapasController < ApplicationController
before_action :set_japa, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
#japas = Japa.all
#users = User.all
end
def show
end
def new
#japa = current_user.japas.build
end
def edit
end
def create
#japa = current_user.japas.build(japa_params)
respond_to do |format|
if #japa.save
format.html { redirect_to #japa, notice: 'Japa was successfully created.' }
format.json { render :show, status: :created, location: #japa }
else
format.html { render :new }
format.json { render json: #japa.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #japa.update(japa_params)
format.html { redirect_to #japa, notice: 'Japa was successfully updated.' }
format.json { render :show, status: :ok, location: #japa }
else
format.html { render :edit }
format.json { render json: #japa.errors, status: :unprocessable_entity }
end
end
end
def destroy
#japa.destroy
respond_to do |format|
format.html { redirect_to japas_url, notice: 'Japa was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_japa
#japa = Japa.find(params[:id])
end
def correct_user
#japa = current_user.japas.find_by(id: params[:id])
redirect_to japas_path, notice: "Not authorized to edit this japacount" if #japa.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def japa_params
params.require(:japa).permit(:rounds, :comment, :user_id)
end
end
Japa Model:
class Japa < ActiveRecord::Base
belongs_to :user
end
User Model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :japas
end
Here is my index template for Japas:
<p id="notice"><%= notice %></p>
<center>
<h1>Listing Japa</h1>
<table class = 'table table-hover' style = 'width:700px;'>
<thead>
<tr>
<th>Time</th>
<th>Rounds</th>
<th>Goal</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.japas.created_at %></td>
<td><%= user.japas.rounds %></td>
<td> / 16 </td>
<td><%= link_to 'Show', japa %></td>
<% if japa.user == current_user %>
<td><%= link_to 'Edit', edit_japa_path(japa) %></td>
<td><%= link_to 'Destroy', japa, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<%end%>
</tr>
<tr>
<td>Total Sum</td>
</tr>
<% end %>
</tbody>
</table>
</center>
<br>
<% if user_signed_in? %>
<%= link_to 'New Japa', new_japa_path %>
<%end%>
Here is my error message when I load: http://localhost:3000/japas.
NoMethodError in Japas#index
Showing /Users/vvd/Desktop/japacounter/app/views/japas/index.html.erb where line #20 raised:
undefined method `created_at' for #<User:0x007f981a4536e8>
Extracted source (around line #20):
<% #users.each do |user| %>
<tr>
<td><%= user.japas.created_at %></td>
<td><%= user.japas.rounds %></td>
<td> / 16 </td>
This is a picture of the error page I am receiving:

Based on your requirements on comments you can try this on.
You have to change your controller action as following for getting japas for the current_user
def index
#japas = Japa.where(user_id: current_user.id)
end
And in view iterate with #japas, as following
<% #japas.each do |japa| %>
<tr>
<td><%= japa.created_at %></td>
<td><%= japa.rounds %></td>
</tr>
<% end %>

You can not call an attribute/method like created_at with an active record associations collection. User has many Japa, so you can re-write you view as following in error area with rest of your code. Just iterate within user.japas.
<% #users.each do |user| %>
<% user.japas.each do |japa| %>
<tr>
<td><%= japa.created_at %></td>
<td><%= japa.rounds %></td>
</tr>
<% end %>
<% end %>

Related

NoMethodError (undefined method `persist' for #<PersonalTrainer:0x000055625dbfd7a8> when trying to create a table entry

I am trying to add items to a table in rails but getting the error it the title. It works for me locally but on heroku it gives me this error. I have seen a couple of solution but none seem to work. This is my controller:
require './app/policies/personal_trainer_policy'
class PersonalTrainersController < ApplicationController
before_action :set_personal_trainer, only: [:show, :edit, :update, :destroy]
# GET /personal_trainers
# GET /personal_trainers.json
def index
#personal_trainers = PersonalTrainer.all
end
# GET /personal_trainers/1
# GET /personal_trainers/1.json
def show
end
# GET /personal_trainers/new
def new
#personal_trainer = PersonalTrainer.new
end
# GET /personal_trainers/1/edit
def edit
end
def personal_trainer_policy
#_personal_trainer_policy ||= PersonalTrainerPolicy.new(personal_trainer)
end
# POST /personal_trainers
# POST /personal_trainers.json
def create
#personal_trainer = PersonalTrainer.new(personal_trainer_params)
authorize #personal_trainer
if #personal_trainer.persist
render json: #personal_trainer.record
else
render json: #personal_trainer.errors, status: :unpocessably_entity
end
respond_to do |format|
if #personal_trainer.save
format.html { redirect_to #personal_trainer, notice: 'Personal trainer was successfully created.' }
format.json { render :show, status: :created, location: #personal_trainer }
else
format.html { render :new }
format.json { render json: #personal_trainer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /personal_trainers/1
# PATCH/PUT /personal_trainers/1.json
def update
authorize #personal_trainer
respond_to do |format|
if #personal_trainer.update(personal_trainer_params)
format.html { redirect_to #personal_trainer, notice: 'Personal trainer was successfully updated.' }
format.json { render :show, status: :ok, location: #personal_trainer }
else
format.html { render :edit }
format.json { render json: #personal_trainer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /personal_trainers/1
# DELETE /personal_trainers/1.json
def destroy
authorize #personal_trainer
#personal_trainer.destroy
respond_to do |format|
format.html { redirect_to personal_trainers_url, notice: 'Personal trainer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_personal_trainer
#personal_trainer = PersonalTrainer.find(params[:id])
end
# Only allow a list of trusted parameters through.
def personal_trainer_params
params.require(:personal_trainer).permit(:firstName, :secondName, :desription, :amountOfClients)
end
end
The controller was generated using a scaffold. This is my erb file:
<p id="notice"><%= notice %></p>
<h1 class = "title">Personal Trainers</h1>
<div class = "page-conatiner">
<table class = "table">
<thead>
<%= stylesheet_link_tag 'gym_classes', media: 'all', 'data-turbolinks-track': 'reload' %>
<tr>
<th>First Name</th>
<th>Second Name</th>
<th>Desription</th>
<th>Amount of Clients</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #personal_trainers.each do |personal_trainer| %>
<tr>
<td><%= personal_trainer.firstName %></td>
<td><%= personal_trainer.secondName %></td>
<td><%= personal_trainer.desription %></td>
<td><%= personal_trainer.amountOfClients %></td>
<td><%= link_to 'Show', personal_trainer %></td>
<td><%= link_to 'Edit', edit_personal_trainer_path(personal_trainer) %></td>
<td><%= link_to 'Destroy', personal_trainer, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if current_user.admin == true %>
<%= link_to 'New Personal Trainer', new_personal_trainer_path %>
<% end %>
</div>
and this is my model:
class PersonalTrainer < ApplicationRecord
has_many :gym_class_final
has_many :pt_client
end
I have it set so that admins can only create new items using a policy and adding authorise #personal_trainer. I wonder if this has something to do with the error. I just dont understand how it would work locally but not when its deployed. Any suggestions.
persist is not a valid method on an active record object, you're looking for save:
if #personal_trainer.save
render json: #personal_trainer.record
else
render json: #personal_trainer.errors, status: :unpocessably_entity
end
For more information on what methods are available for active record persistence, see https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html (Rails 6)

Running into undefined method from model class in view

I have a method called calculation_of_total_cost in model Tippy
It's running into problems being called in index.html.erb via tippies views directory.
This is the error I receive: undefined method*' for nil:NilClass`
I have googled it, and now understand that it is the result of the one of the variables being nil.
How do I resolve this, i.e, how do I make the method work in index.html.erb? This is index view that I am calling it from, so I need an instance method, not class, right?
Also, addendum: this same method works fine in show.html.erb
show.html.erb
<br/><br/>
<h1 class="text-center">Your Total Cost</h1>
<br/><br />
<table class="table table-striped">
<tr>
<td>
Cost of Your Meal:
</td>
<td>
<%= humanized_money_with_symbol #tippy.cost %>
</td>
</tr>
<tr>
<td>
Tip You Picked:
</td>
<td>
<%= number_to_percentage(#tippy.tip * 100, format: "%n%", precision: 0) %>
</td>
</tr>
<tr>
<td>
The Total Cost:
</td>
<td>
<%= humanized_money_with_symbol #tippy.calculation_of_total_cost %>
</td>
</tr>
</table>
<%= link_to 'New Tippy', new_tippy_path %>
<%= link_to "Index", tippies_path %>
Here is the Tippy model:
class Tippy < ApplicationRecord
validates :tip, presence: true
validates :cost, presence: true
#monetize :tip_cents
monetize :cost_cents, :numericality => {:greater_than => 0}
TIP_CHOICES = { "10%" => ".10", "20%" => ".20", "30%" => ".30", "40%" => ".40", "50%" => ".50",
"60%" => ".60", "70%" => ".70", "80%" => ".80", "90%" => ".90" }
def calculation_of_total_cost
cost + (tip * cost)
end
end
Here is the index.html.erb file
<p id="notice"><%= notice %></p>
<h1>Tippies</h1>
<table>
<thead>
<tr>
<th>Tip</th>
<th>Cost</th>
<th>Total</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tippies.each do |tippy| %>
<tr>
<td><%= tippy.tip %></td>
<td><%= tippy.cost %></td>
<td><%= tippy.calculation_of_total_cost %></td>
<td><%= link_to 'Show', tippy %></td>
<td><%= link_to 'Edit', edit_tippy_path(tippy) %></td>
<td><%= link_to 'Destroy', tippy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Tippy', new_tippy_path %>
Tippy Controller
class TippiesController < ApplicationController
#before_action :set_tippy, only: [:show, :edit, :update, :destroy]
# GET /tippies
# GET /tippies.json
def index
#tippies = Tippy.all
end
# GET /tippies/1
# GET /tippies/1.json
def show
##calculation_of_total_cost
end
# GET /tippies/new
def new
#tippy = Tippy.new
end
# GET /tippies/1/edit
def edit
end
# POST /tippies
# POST /tippies.json
def create
#tippy = Tippy.new(tippy_params)
respond_to do |format|
if #tippy.save
format.html { redirect_to #tippy, notice: 'Tippy was successfully created.' }
format.json { render :show, status: :created, location: #tippy }
else
format.html { render :new }
format.json { render json: #tippy.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tippies/1
# PATCH/PUT /tippies/1.json
def update
respond_to do |format|
if #tippy.update(tippy_params)
format.html { redirect_to #tippy, notice: 'Tippy was successfully updated.' }
format.json { render :show, status: :ok, location: #tippy }
else
format.html { render :edit }
format.json { render json: #tippy.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tippies/1
# DELETE /tippies/1.json
def destroy
#tippy.destroy
respond_to do |format|
format.html { redirect_to tippies_url, notice: 'Tippy was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tippy
#tippy = Tippy.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tippy_params
params.require(:tippy).permit(:tip, :cost)
end
end
To solve this problem you need to set a binding.pry or a breakpoint at this line of index.html.erb, so that we can understand in the loop you are executing why tippy is getting value of nil.
You need to install pry gem.
Please also share the values of #tippies and the details of the other variable in the loop that fails, because tippy=nil.
An alternative for pry is just printing the value of the variable in the log with puts tippy.calculation_of_total_cost.
Right now I am guess is that #tippies which includes all #tippy in your tippies table, could have one field that has calculation of total cost = nil. To verifiy this you should check with the debug the value of tippy and of tippy.calculation_of_total_cost in the index.html.erb view.
<% #tippies.each do |tippy| %>
<tr>
<% binding.pry %>
<td><%= tippy.tip %></td>
<td><%= tippy.cost %></td>
<td><%= tippy.calculation_of_total_cost %></td>
<td><%= link_to 'Show', tippy %></td>
<td><%= link_to 'Edit', edit_tippy_path(tippy) %></td>
<td><%= link_to 'Destroy', tippy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
also it is a good idea to inspect show.html.erb as there it is working.
def calculation_of_total_cost
cost + (tip * cost)
end

ArgumentError in Shakes#index , nil is not a valid asset source

Hi guys when iam trying to pull image using image tag its giving nil is not a valid asset source ive image file in my assests , strangely this problem is only occuring when iam trying use decorator pattern else without it image is being properly displaye ,Iam fairly new to this can any of the experts shred some light what am i doing wrong ?
Thanks
require 'shake_decorator'
class ShakesController < ApplicationController
before_action :set_shake, only: [:show, :edit, :update, :destroy]
# GET /shakes
# GET /shakes.json
def index
#shakes = Shake.all
end
# GET /shakes/1
# GET /shakes/1.json
def show
end
# GET /shakes/new
def new
#shake = Shake.new
end
# GET /shakes/1/edit
def edit
end
# POST /shakes
# POST /shakes.json
def create
#shake = Shake.new()
#shake.Name=params[:shake][:Name]
#shake.Cost=params[:shake][:Cost]
#shake.Calories=params[:shake][:Calories]
# create an instance/object of a BasicCar
myShake=BasicShake.new(#shake.Cost,#shake.Calories)
#add the extra features to the new car
if params[:shake][:caramel].to_s.length > 0 then
myShake = CaramelDecorator.new(myShake)
end
if params[:shake][:pbutter].to_s.length > 0 then
myShake = PeanutbutterDecorator.new(myShake)
end
if params[:shake][:cream].to_s.length > 0 then
myShake = CreamDecorator.new(myShake)
end
#populate the cost and the description details
#shake.Cost = myShake.cost
#shake.description = myShake.details
#retrieve the instance/object of the MyLogger class
respond_to do |format|
if #shake.save
format.html { redirect_to #shake, notice: 'Shake was successfully created.' }
format.json { render :show, status: :created, location: #shake }
else
format.html { render :new }
format.json { render json: #shake.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shakes/1
# DELETE /shakes/1.json
def destroy
#shake.destroy
respond_to do |format|
format.html { redirect_to shakes_url, notice: 'Shake was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_shake
#shake = Shake.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def shake_params
params.require(:shake).permit(:Name, :Cost, :Calories, :image_url)
end
end
<p id="notice"><%= notice %></p>
<h1>Shakes</h1>
<table border="2">
<thead>
<tr>
<th>Name</th>
<th>Cost</th>
<th>Calories</th>
<th>Image url</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #shakes.each do |shake| %>
<tr>
<td><%= shake.Name %></td>
<td><%= shake.Cost %></td>
<td><%= shake.Calories %></td>
<td><%= image_tag(shake.image_url, :class => 'list_image' ,:style => "height:100px") %></td>
<td><%= link_to 'Show', shake %></td>
<td><%= link_to 'Edit', edit_shake_path(shake) %></td>
<td><%= link_to 'Destroy', shake, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Shake', new_shake_path %>

ActiveRecord::RecordNotFound in TodosController#index

The full text of the error message is:
ActiveRecord::RecordNotFound in TodosController#index
Couldn't find User with id=2
Rails.root: /home/randy/rubystack-1.9.3-29/projects/chap14
app/controllers/application_controller.rb:11:in current_user
app/views/todos/index.html.erb:21:in _app_views_todos_index_html_erb___949818655437808348_39324440
app/controllers/todos_controller.rb:8:in index
Here's the code for my application_controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
private
def current_user
if session[:user_id]
#current_user ||= User.find(session[:user_id])
else
#current_user = nil
end
end
def check_login
unless authorized?
redirect_to "/auth/identity"
end
end
def logged_in?
if session[:user_id]
return true
else
return false
end
end
protected
def authorized?
logged_in? && (request.get? || current_user.admin?)
end
end."
And here's my todos_controller.rb:
class TodosController < ApplicationController
before_filter :check_login
# GET /todos
# GET /todos.json
def index
#todos = Todo.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #todos }
end
end
# GET /todos/1
# GET /todos/1.json
def show
#todo = Todo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #todo }
end
end
# GET /todos/new
# GET /todos/new.json
def new
#todo = Todo.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #todo }
end
end
# GET /todos/1/edit
def edit
#todo = Todo.find(params[:id])
end
# POST /todos
# POST /todos.json
def create
#todo = Todo.new(params[:todo])
respond_to do |format|
if #todo.save
format.html { redirect_to #todo, notice: 'Todo was successfully created.' }
format.json { render json: #todo, status: :created, location: #todo }
else
format.html { render action: "new" }
format.json { render json: #todo.errors, status: :unprocessable_entity }
end
end
end
# PUT /todos/1
# PUT /todos/1.json
def update
#todo = Todo.find(params[:id])
respond_to do |format|
if #todo.update_attributes(params[:todo])
format.html { redirect_to #todo, notice: 'Todo was successfully updated.' }
format.json { head :no_content }
#todo = Todo.find(params[:id])
if #todo.completed == true
#todo.user_who_completed = current_user.email
#todo.save
end
else
format.html { render action: "edit" }
format.json { render json: #todo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todos/1
# DELETE /todos/1.json
def destroy
#todo = Todo.find(params[:id])
#todo.destroy
respond_to do |format|
format.html { redirect_to todos_url }
format.json { head :no_content }
end
end
end."
And my app/views/todos/index.html.erb file:
"<h1>Listing todos</h1>
<table>
<tr>
<th>Name</th>
<th>Completed</th>
<th>Completed date</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
<% for todo in #todos %>
<tr>
<td><%= todo.name %></td>
<td><%= todo.completed %></td>
<td><%= todo.completed_date %></td>
<td><%= todo.user %></td>
<% end %>
<% if current_user.admin? %>
<td><%= link_to 'Show', todo %></td>
<td><%= link_to 'Edit', edit_todo_path(todo) %></td>
<td><%= link_to 'Destroy', todo, method: :delete, data: { confirm: 'Are you sure?' } %></td>S
</tr>
<% end %>
</table>
<br />
<% if current_user.admin? %>
<%= link_to 'New Todo', new_todo_path %>
<% end %>
I'm new to rails and have no clue about what the error message means, nor its cause. I want the visitor to land on the todos/index.html page. I have my root route set accordingly. Would appreciate some help.
That error is coming from User.find. It's described in the Documentation for find.
It's telling you that there is no User that has that id. Somehow you have saved a user id into your session that corresponds to a user that doesn't currently exist.
You should probably examine how you are storing user ids to the session and under what circumstances that user could be getting deleted.
The problem is with your loop. If conditional it is outside the cycle.
Check solution view:
<h1>Listing todos</h1>
<table>
<tr>
<th>Name</th>
<th>Completed</th>
<th>Completed date</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
<% for todo in #todos %>
<tr>
<td><%= todo.name %></td>
<td><%= todo.completed %></td>
<td><%= todo.completed_date %></td>
<td><%= todo.user %></td>
<% if current_user.admin? %>
<td><%= link_to 'Show', todo %></td>
<td><%= link_to 'Edit', edit_todo_path(todo) %></td>
<td><%= link_to 'Destroy', todo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
</table>
Recommendations
Check your question. You have the code view with the controller code.
Be careful with indentation.

No route matches {:action=>"new", :controller=>"adverts", :userr_id=>nil} missing required keys: [:userr_id]

I am new to ruby on rails and any help would be much appreciated. i am unsure on how to go about resolving my error. I think i am meant to do more in my controller but i am confused as this is a nested resources challenge i am facing
i am trying to view the details of an advert (show) in my views, but when i click the link show i get the below error:
No route matches {:action=>"new", :controller=>"adverts", :userr_id=>nil} missing required keys: [:userr_id]
ActionController::UrlGenerationError (No route matches {:action=>"new", :controller=>"adverts", :userr_id=>nil} missing required keys: [:userr_id]):
app/views/shared/_header_recruiter.html.erb:6:in `_app_views_shared__header_recruiter_html_erb___2254802171992378619_70258749378740'
app/views/adverts/show.html.erb:1:in `_app_views_adverts_show_html_erb___3448815807687044417_70258749644120'
app/controllers/adverts_controller.rb:26:in `show'
i have adverts nested under userr (recruiters) - routes.rb
Rails.application.routes.draw do
devise_for :userrs
resources :userrs do
resources :adverts
end
devise_for :userjs
root 'static_pages#homepg'
get 'search', to: 'static_pages#searchpg'
end
i created a static page called searching - static_pages_controller.rb
class StaticPagesController < ApplicationController
respond_to :html, :xml, :json
def searchpg
#adverts = Advert.all
end
end
i have the below codings in my searching view file - searchpg.html.erb
<div>
<table>
<thead>
<tr>
<th>Published</th>
<th>Title</th>
<th>Content</th>
<th>City</th>
<th></th>
</tr>
</thead>
<tbody>
<% #adverts.each do |advert| %>
<tr>
<td><%= advert.created_at.strftime("%B %d, %Y") %></td>
<td><%= link_to advert.title, '#' %></td>
<td><%= advert.content %></td>
<td><%= advert.city %></td>
<td><%= link_to 'Show', userr_advert_path(advert.userr, advert) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
theses are my routes:
userr_adverts GET /userrs/:userr_id/adverts(.:format) adverts#index
POST /userrs/:userr_id/adverts(.:format) adverts#create
new_userr_advert GET /userrs/:userr_id/adverts/new(.:format) adverts#new
edit_userr_advert GET /userrs/:userr_id/adverts/:id/edit(.:format) adverts#edit
userr_advert GET /userrs/:userr_id/adverts/:id(.:format) adverts#show
PATCH /userrs/:userr_id/adverts/:id(.:format) adverts#update
PUT /userrs/:userr_id/adverts/:id(.:format) adverts#update
DELETE /userrs/:userr_id/adverts/:id(.:format) adverts#destroy
i already have an adverts controller:
class AdvertsController < ApplicationController
respond_to :html, :xml, :json
before_action :set_advert, only: [:show, :edit, :update, :destroy]
def index
#userr = Userr.find(params[:userr_id])
#adverts = #userr.adverts.order("created_at DESC")
respond_with(#adverts)
end
def show
#userr = Userr.find(params[:userr_id])
#advert = #userr.adverts.find(params[:id])
respond_with(#advert)
end
def new
#userr = Userr.find(params[:userr_id])
#advert = #userr.adverts.build
respond_with(#advert)
end
def edit
#userr = Userr.find(params[:userr_id])
#advert = #userr.adverts.find(params[:id])
end
def create
#userr = Userr.find(params[:userr_id])
#advert = #userr.adverts.create(advert_params)
respond_to do |format|
if #advert.save
format.html { redirect_to([#advert.userr, #advert], notice: 'Advert was successfully created.') }
format.json { render json: #advert, status: :created, location: #advert }
else
format.html { render action: "new" }
format.json { render json: #advert.errors, status: :unprocessable_entity }
end
end
end
def update
#userr = Userr.find(params[:userr_id])
#advert = #userr.adverts.find(params[:id])
respond_to do |format|
if #advert.update_attributes(advert_params)
format.html { redirect_to([#advert.userr, #advert], notice: 'Advert was successfully updated.') }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #advert.errors, status: :unprocessable_entity }
end
end
end
def destroy
#userr = Userr.find(params[:userr_id])
#advert = #userr.adverts.find(params[:id])
#advert.destroy
respond_to do |format|
#1st argument reference the path /recruiters/:recruiter_id/adverts/
format.html { redirect_to(recruiter_adverts_url) }
format.json { head :no_content }
end
end
private
def set_advert
#advert = Advert.find(params[:id])
end
def advert_params
params.require(:advert).permit(:title, :content, :category_jobtype_id, :category_positiontype_id, :salarystart, :salaryend, :category_country_id, :city, :town, :postcode, :category_editorialapproval_id, :category_applicationrequest_id, :category_advert_id, :userr_id )
end
end
The error notifies that There is no userr_id in the url which means that advert object does not have userr object or you may forgot to enter userr_id while manually typing the url.
It seems like you don't have a advert.userr object. The easiest way to check that is to print it.
So temporarily, remove your link and print the userr
<% #adverts.each do |advert| %>
<tr>
<td><%= advert.created_at.strftime("%B %d, %Y") %></td>
<td><%= link_to advert.title, '#' %></td>
<td><%= advert.content %></td>
<td><%= advert.city %></td>
<td><%#= link_to 'Show', userr_advert_path(advert.userr, advert) %></td>
<td><%= advert.userr.inspect %></td>
</tr>
<% end %>
So if you can;t see anything, that means your relations between userr and advert is not setup correctly.

Resources