Couldn't find tasksadmins with id=15 - ruby-on-rails

I have two controllers: Tasksadmins and Workers.
I defined a table, is called: Tasksadmin, and has the parameters: admin_email, worker_email, task, done. the done option is check box.
in Tasksadmins controller, I defined:
def edit
#tasksadmin = Tasksadmin.find(params[:id])
end
in this controller, I can edit all the row of the table. it works fine!
in the Workerscontroller, I tried to define:
def edit
#tasksadmin = Tasksadmin.find(params[:id])
end
I did it because I want to get all of the parameters of the row, but I will let the worker an option to change only the parameter of done.
unfortunately, I got this error:
ActiveRecord::RecordNotFound in WorkersController#edit
Couldn't find Worker with id=15
I think I got it because the url is: localhost:3000/workers/15/edit
(pay attention to: workers/15/edit)
in order to understand where the problem is, I wrote the next word in my models/workers/edit.html.erb:
hello!
so I think the problem is with the work workers in the url :/
how can I fix it please?
UPDATE:
this is the index of the workerscontroller:
def index
#tasks_worker = Tasksadmin.where(:worker_mail => current_user.email)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #workers }
end
end
this is index.html.erb:
<h1>Listing workers</h1>
<table>
<tr>
<th>Admin_mail</th>
<th>Task</th>
<th>Done</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #tasks_worker.each do |task| %>
<tr>
<td><%= task.admin_mail %></td>
<td><%= task.task %></td>
<td><%= task.done %></td>
<td><%= link_to 'Edit', edit_worker_path(task) %></td>
</tr>
<% end %>
</table>

It seems like Worker with ID=15 doesn't exist.
Worker.find(params[:id])
Will raise ActiveRecord::RecordNotFound if that Worker doesn't exist.
You can go into the rails console and try:
Worker.find(15)
If you want to have find return nil instead of raising you can use:
Worker.find_by_id(params[:id])

I'm not too sure about this (maybe somebody can correct me), but I think
#tasksadmin = Tasksadmin.find(params[:id])
just defines the variable #tasksadmin.
Now, if you call the localhost:3000/workers/15/edit-URL, Rails is actually searching for a worker with ID 15 (Worker.find(15))
What do you want to accomplish?

I added this line in Workerscontroller:
load_and_authorize_resource :except => [:edit, :update]
and this line in Tasksadminscontroller:
load_and_authorize_resource :except => [:update, :show]

Related

Deleting an entire entry from your databse

I am trying to create a delete button on my show page that shows the data of the selected recipe. My problem is every time I try to make a delete button i keep either getting path errors like "cant find the path recipe_path" or an error that says
/home/robert/recipe/app/views/recipes/show.html.erb:41: syntax error, unexpected tIDENTIFIER, expecting ')' ...pend=( link_to 'Delete' recipes, method: :delete );#output_b... ... ^~~~~~~"
if you need any more code or information please ask.
I have tried changing my path looking at others that had delete issues like this and watching several videos on deleting in ruby on rails but none of it has worked.
<button><%= link_to 'Delete' recipes, method: :delete
%></button>
def destroy
#recipes = Recipe.find(params[:id])
#recipes.destroy
end
for some permutations of the delete buttons I have made I have gotten either the error messages stated above or a button shows up that does nothing.
You are missing a comma between 'Delete' recipes in the arguments list:
<%= link_to 'Delete', recipes, method: :delete %>
Which solves the syntax error. However placing an <a> inside a <button> tag is invalid HTML.
Permitted content
Phrasing content but there must be no Interactive content
- MDN Web Docs: The Button element
Either just style a regular <a> with CSS to look like a button or create a discrete form with button_to:
<%= button_to 'Delete', recipes, method: :delete %>
Since you seem to be just flailing around you can get an example of the how to actually code this by running the scaffold generator.
$ rails g scaffold recipe title:string
Which generates:
# config/routes.rb
Rails.application.routes.draw do
resources :recipes
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
# app/controllers/recipes_controller.rb
class RecipesController < ApplicationController
before_action :set_recipe, only: [:show, :edit, :update, :destroy]
# GET /recipes
# GET /recipes.json
def index
#recipes = Recipe.all
end
# ...
# DELETE /recipes/1
# DELETE /recipes/1.json
def destroy
#recipe.destroy
respond_to do |format|
format.html { redirect_to recipes_url, notice: 'Recipe was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_recipe
#recipe = Recipe.find(params[:id])
end
# ...
end
# app/views/recipes/index.html.erb
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #recipes.each do |recipe| %>
<tr>
<td><%= link_to 'Show', recipe %></td>
<td><%= link_to 'Edit', edit_recipe_path(recipe) %></td>
<td><%= link_to 'Destroy', recipe, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
Note that in Rails proper pluralization is very important as it ties in with the whole convention over configuration approach.

Facing issues with link_to "Edit" in rails

I am trying to display all the metrics in a table format with an edit option. But, I end up with the below error
In Index view, I am able to see all the data. But when I click on edit link, it is not redirecting to edit view where I have different columns to be showed.
index view:
<%= form_for :metrics_controller, url: metrics_path(#metric), method: :get do |f| %>
<table id="metrics">
<thead>
<tr id="AllMetricColumnNames">
<th id="CommentsColumn">Comments</th>
<th id="EditColumn">Edit</th>
</tr>
</thead>
<% #metricAll.each do |data| %>
<tr id="AllMetricValues">
<td id="Comments"><%= data.Comments %></td>
<td id="EditButton"><%= link_to "Edit", edit_metric_path(#metricAll) %></td>
<% end %>
</tr>
</table>
<% end %>
Controller:
class MetricsController < ApplicationController
def index
#metricAll = Metric.all
end
def show
#metric = Metric.find(params[:id])
end
def edit
#metric = Metric.find(params[:id])
end
private def post_params
params.require(:metric).permit(:Metric, :Comments)
end
end
routes:
root 'metrics#index'
get 'index' => 'metrics#index'
get 'edit' => 'metrics#edit'
resources :metrics
You're passing ALL the metrics for the edit route. Move from
<td id="EditButton"><%= link_to "Edit", edit_metric_path(#metricAll) %></td>
to
<td id="EditButton"><%= link_to "Edit", edit_metric_path(data) %></td>
data is the current metric in your code
According to your screenshot, the error is within the model.
Also, as mentioned by others, you should remove those get routes as the resources :metrics will generate the necessary routes for all your CRUD actions a.ka. for the index, show, edit, new, create, update, destroy.
My guess is that the metric.rb file has a belongs_to :automated_thresholding relationship but the metrics database table is missing the field automated_thresholding_id.
You should create a migration to add that field
add_reference :metrics, :automated_thresholding

Rails undefined method `each' for nil:NilClass...but it is defined

I have searched for hours, tried every possible fix. I cannot make this work. The error is:
*NoMethodError in Articles#index
Showing /Users/myname/blog/app/views/articles/showall.html.erb where line #21 raised:
undefined method `each' for nil:NilClass*
showall.html.erb is a view. It is rendered from the 'article' controller. (both posted below). There is a route to showall, and it works fine. Currently the route is configured as:
get 'article/showall'
But, I have also tried it as:
resources :articles do
get 'showall'
resources :comments
Both routes worked, but neither had an effect on the issue.
There is a method in the controller, its not private:
def showall
#articles = Article.all
end
The offending piece of code in the view is:
<% #articles.each do |article| %>
<tr>
<td><%= article.title.truncate(30) %></td>
<td><%= article.author %></td>
<td><%= article.manufacturer %></td>
<td><%= article.model %></td>
<td><%= article.displacement %></td>`
<% end %>
I actually cut and pasted, the piece of code from the index.html.erb view, where it works perfectly. I have tried every nuance of pluralization I can think of. Any help would be greatly appreciated.
This applicable parts of the controller:
class ArticlesController < ApplicationController
skip_before_action :authorize, only: [:index, :show, :showall]
#Filter used to catch nonlogged in users
before_filter :require_user, :only => [:index]
#method that checks if logged in, sends them to showall if not.
def require_user
unless User.find_by(id: session[:user_id])
render 'showall', :notice => "Please log in to read articles."
end
end
def index
#articles = current_user.articles
end
#should list articles, but throws undefined method 'each' error
def showall
#articles = Article.all
end
Here is the entire view:
<%= render "menu" %>
<body>
<font color="yellow"><%= flash[:notice] %></font>
<br>
<font color="grey">Motorcycle Articles</font>
<%= link_to 'Post New Article', new_article_path %>
<br>
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Brand</th>
<th>Model</th>
<th>Displacment</th>
<th>Last Edited On:</th>
<th>Article</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title.truncate(30) %></td>
<td><%= article.author %></td>
<td><%= article.manufacturer %></td>
<td><%= article.model %></td>
<td><%= article.displacement %></td>
<% end %>
</table>
<br>
All articles are property of their respective owners.
</body>
Route is triggering index action, see:
NoMethodError in Articles#index
You get the error because current_user.articles is nil.
You need to make sure Articles#showall is appearing in the log, that will mean that showall method is called.
Do that creating routes:
get '/articles', to: 'Articles#showall'
resources :articles
This is not recommended. There are several parts to improve. But it should make the error disappear.
You are calling render 'showall', which renders the view. This is different than 'redirect_to', which calls the controller action. Since you are setting the value of #articles with a nil value (current_user is not set), you get this error.
To clarify, you need to either redirect_to the 'showall' action or redefine #articles to equal Article.all before rendering the view. Personally I'd redirect.
Modify your routes file
routes.rb
resources :articles do
collection do
get 'showall'
end
end

Editing multiple records in one form - Rails

I'm trying to edit multiple records using one form, so the user can edit a few records then press submit at the end rather than after each individual one. I've posted my current code, and I get this error:
undefined method `connection_connection_path'
Controller
def show
#customer = Customer.find(params[:id])
#connection = #customer.connections
respond_to do |format|
format.html # show.html.erb
format.json { render json: #customer }
end
end
View
<table class="table">
<thead>
<th>Interface</th>
<th>Device</th>
<th>Speed</th>
<th>Site</th>
<th>Capable</th>
<th>Notes</th>
</thead>
<%= form_for(#connection) do |f| %>
<% #connection.each do |l| %>
<tr>
<td><%= l.interface %></td>
<td><%= l.device %></td>
<td><%= l.speed %></td>
<td><%= l.site.name%> </td>
<td><%= f.check_box :check %></td>
<td><%= f.text_field :notes %></td>
</tr>
<% end %>
<tr><%= f.submit %></tr>
</table>
<% end %>
routes
resources :connections
resources :sites
resources :customer_sites
resources :customers
root :to => "customers#index"
so where is your submit button?? and it should not done in show, maybe you might create a new method in your controller and do the multiple edit function.
The basic concept is when you have checks, you need to pass an array of your object to your method, and update them one by one. or you can do that using JS as well.
refer this railscast
http://railscasts.com/episodes/165-edit-multiple
and this stackoverflow
Rails 3 Edit Multiple Records in a Single Form
these resources you can get easily by google :D
if still have problem, just come and ask here again

How can I implement vanity URL's in a Rails application?

I want users to able to have a profile page at site.com/myNameHere. Rails looks for a controller named "myNameHere." Is it possible to setup routes.rb so that if the controller is not found, "myNameHere" gets sent as a parameter to another controller?
You can add a route like this:
map.profile_link '/:username', :controller => 'profiles', :action => 'show'
Be sure to add it low enough in the file, below your resources, that it doesn't interfere with other routes. It should be lowest priority. Next, you need to change your show action to use a username instead of id:
def show
#user = User.find_by_username(params[:username])
end
That's all there is to it. Happy coding!
UPDATE:
I've expanded this answer into a full blog post, Vanity URLs in Ruby on Rails Routes. I have additional code samples and a more thorough explanation.
Just thought I would update this for Rails 3. I have a field 'website' in my user model that is going to act as the vanity route, and I am going to route to a static page for each vanity route. I want to display all of a users 'events' if the vanity route is called.
#config/routes.rb
get '/:website', :controller => 'static_pages', :action => 'vanity'
#app/controllers/static_pages_controller.rb
def vanity
#user = User.find_by_website(params[:website])
if #user != nil #in case someone puts in a bogus url I redirect to root
#events = #user.events
else
redirect_to :root
end
end
#app/views/static_pages/vanity.html.erb
<h1><%= #user.website %> events</h1>
<table>
<tr>
<th>Title</th>
<th>Starts at</th>
<th>Description</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #events.each do |event| %>
<tr>
<td><%= event.title %></td>
<td><%= event.starts_at %></td>
<td><%= event.description %></td>
</tr>
<% end %>
</table>
I think this is a good alternative if you don't want to use a gem.

Resources