I am having a problem deleting clients I have created in rails. I am using rails 6.0
"undefined method `destroy' for nil:NilClass"
Controller:
class ClientsController < ApplicationController
before_action :set_client, only: [:show, :edit, :update, :destroy]
def destroy_multiple
#client.destroy(params[:client_ids])
respond_to do |format|
format.html { redirect_to clients_url, notice: 'Client was successfully destroyed.' }
format.json { head :no_content }
end
end
View:
<div class="container">
<div class="table-responsive">
<%= form_tag destroy_multiple_clients_path, method: :delete do %>
<table class="table table-hover">
<thead>
<tr class="table-secondary">
<th scope="col" ><input type="checkbox"></th>
<th>Name</th>
<th>Email</th>
<th>Phone number</th>
<th>Client Type</th>
<th>Nickname</th>
</tr>
</thead>
<tbody>
<% #clients.each do |client| %>
<tr>
<td><%= check_box_tag "client_ids[]", client.id %></td>
<td><%= link_to client.name, client, :class => "clientname" %></td>
<td><%= client.email %></td>
<td><%= client.phone_number %></td>
<td><%= client.client_type %></td>
<td><%= client.nickname %></td>
<% end %>
</tbody>
</table>
</div>
<hr class="featurette-divider">
<%= submit_tag "Delete selected", :class => 'btn btn-primary btn-xs' %>
Console:
Parameters: {"authenticity_token"=>".................==", "client_ids"=>["11"], "commit"=>"Delete selected"}
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.0ms | Allocations: 1019)
NoMethodError (undefined method `destroy' for nil:NilClass):
app/controllers/clients_controller.rb:65:in `destroy_multiple'
Not sure what I am missing here? How can I pass the ID that shows up in the console to the controller? The examples online show what I am doing but it isn't working for me.
You are using an instance variable that is nil. The destroy method should use the class method
e.g.
instead of
#client.destroy(params[:client_ids])
you could try
Client.destroy(params[:client_ids])
with regards to your question
How can I pass the ID that shows up in the console to the controller?
You are already doing that as evidenced in your stack trace
Parameters: {"authenticity_token"=>".................==", "client_ids"=>["11"], "commit"=>"Delete selected"}
You can clearly see the client_ids parameter is being passed an array, hence the square brackets [] containing a single element with the value of 11 hence the =>["11"]
Related
I'm trying to get the data from the phrases_term model to the view, but I'm getting an error.
Phrases_term model:
Phrases_term(id, term_id, phrase_id)
phrases_terms_controller.rb
class PhrasesTermsController < ApplicationController
before_action :authenticate_user!
before_action :set_term
def new
#phrases_term = PhrasesTerm.new
end
def create
#phrases_term = #term.phrases_terms.new(phrases_term_params)
if #phrases_term.save
redirect_to term_phrases_term_path(#term, #phrases_term), notice: "Phrases_Term was successfully created"
else
render "new"
end
end
private
def phrases_term_params
params.require(:phrases_term).permit(:term_id, :phrase_id)
end
def set_term
#term = Term.find(params[:term_id])
end
end
Phrases_term show view, show.html.erb:
<div class="container">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Phrases_ID</th>
<th>Term_ID</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= #phrases_term.id %></td>
<td><%= #phrases_term.term_id %></td>
<td><%= #phrases_term.phrase_id %></td>
</table>
<%= link_to 'Back', terms_path %> |
<%= link_to 'Edit', edit_term_path %>
</div>
The error I'm getting:
Error: Eundefined method `id' for nil:NilClass
You're getting an undefined method 'id' for nil:NilClass error because your show.html.erb view doesn't know what #phrases_term is. Hence calling the id method on a variable that doesn't exist yet throws an error. You need to define the variable you're using in the right controller under the right action. So adding:
def show
#phrases_term = PhrasesTerm.find(params[:id])
end
in you phrases_terms_controller.rb should solve the error.
I was trying to override devise default edit method. Normally, by default, it can only edit current_user. But I want to edit any user I want. That's why I coded like this:
IN CONTROLLER:
def edit
#user = User.find(params[:id])
end
And it is called from the show, where I called this function like this:
SHOW.HTML.ERB:
<%= link_to user_profiles_edit_path(#user.id) do%>
<button class="btn btn-info">Edit Profile</button>
<% end %>
And in edit, I had a form, which starts like this:
EDIT.HTML.ERB:
<%= form_for(#user) do |f| %>
<table class="table table-striped">
<tr>
<th>First Name</th>
<td><%= f.text_field(:first_name, :class=>"full-width-input") %></td>
</tr>
<tr>
<th>Last Name</th>
<td><%= f.text_field(:last_name, :class=>"full-width-input") %></td>
</tr>
...
...
...
And I added this line in my routes.rb:
ROUTES.RB
get "/users/:id/edit/" => "users/registrations#edit",:as =>"user_profiles_edit"
But, After doing all these, I am getting this error:
undefined method `user_path' for #<#<Class:0x007f3da064c050>:0x007f3da065adf8>
Did you mean? users_path
I've been banging my head against this for a while. I know it's a simple problem, and I've reviewed other code examples where I'm doing this successfully, but, I'm completely stumped.
I'm getting an error "undefined method `page_leads' for nil:NilClass" when I try to go to the "Show" page. On my landing_pages "show" page I'm trying to show the leads that came in via that page.
My show page code for this is:
<table class="table table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Score</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #landingpage.page_leads.each do |page_lead| %>
<tr>
<td><%= page_lead.fname %></td>
<td><%= page_lead.lname %></td>
<td><%= page_lead.score %></td>
<td><%= link_to 'Show', page_lead %></td>
<td><%= link_to 'Edit', edit_page_lead_path(page_lead) %></td>
<td><%= link_to 'Destroy', page_lead, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
My landing_page model has:
has_many :page_leads
My page_lead model has:
belongs_to :landing_page
My controller code for the "show" method for both landing_pages and page_lead is:
def show
#landing_page = LandingPage.all(landing_page_params)
end
On my page_leads table I have the "landing_page_id" field so I can associate the landing page to the lead.
Any ideas what I'm doing wrong?
Thanks in advance
Your controller action does not load any instance of a model. You expect it to load an instance of a LandingPage (usually the one in params[:id] for a show action). So your controller should assign it:
# `GET /landing_pages/:id`
def show
#landingpage = LandingPage.find( params[:id] )
end
It is because your #landingpage instance variable is not defined when you run that code.
Basically, in your controller's action, you should have something like:
def show
#landinpage = ... # insert your definition here
# rest of your controller's action
end
I'm currently using Rails 3.2 with the activerecord-oracle_enhanced-adapter gem. I am connecting to an already existing table in an already existing database. I have created the controller, model, and view files myself (they were not autogenerated).
When I start up a rails server and navigate to localhost:3000/devices, I get the following error - "undefined method `accept' for nil:NilClass".
The full stack trace is here:
http://pastebin.com/YfiM7W7S
My MVC for device are printed below:
Model:
class Device < ActiveRecord::Base
self.table_name="DEVICE"
self.primary_key="DEVICE_NAME_ID"
end
View:
<h1>Listing devices</h1>
<table>
<thead>
<tr>
<th>Device Name ID</th>
<th>Device Desc</th>
<th>Agg Flg</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #devices.each do |device| %>
<tr>
<td><%= device.device_name_id %></td>
<td><%= device.device_desc %></td>
<td><%= device.agg_flg %></td>
<td><%= link_to 'Show', device %></td>
<td><%= link_to 'Edit', edit_device_path(device) %></td>
<td><%= link_to 'Destroy', device, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Device', new_device_path %><head>
Controller:
class DevicesController < ApplicationController
before_filter :set_device, only: [:show, :edit, :update, :destroy]
# GET /devices
# GET /devices.json
def index
#devices = Device.all
end
private
# Use callbacks to share common setup or constraints between actions.
def set_device
#device = Device.find(params[:id])
end
end
Any help would be greatly appreciated. If you need any more information, please let me know.
I'm following the tutorial found here. It's simple, and I've followed the instructions exactly to step 6.7. At this point, I get the error
undefined method `each' for nil:NilClass
when I try to access index.html.erb on the rails server.
I know the database is working fine, because I can do everything mentioned in step 6.3, create new posts and show/edit/destroy them with absolutely no problems.
Specifically, the issue is with the line
<% #posts.each do |post| %>
and it's essentially claiming that #posts is nil.
I appreciate any help for this ROR newbie! Thanks.
index.html.erb
<h1>Hello, Rails!</h1>
<table>
<tr>
<th>Name</th>
<th>Title</th>
<th>Content</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
:method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to "My Blog", posts_path %>
posts_controller.rb
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
In order for the views to work fine in Rails they must be inside the correct directory. This is one of the many implementations of the so called "Convention over Configuration" that Rails loves.
So, if you have a method index and this method is inside a controller named PostsController, you must have a view named index inside the directory views/posts/. This way, Rails will know that it have to render this view when a get to this method is processed.
About a good tutorial, I would recommend this one. It is extense and covers a lot of things that are not just related to Rails itself, like deploying on Heroku and a little CSS.