Rails nested Resources Error - ruby-on-rails

I have this nested recourses
resources :products do
resources :senders
end
In my products/index view I have this
...
..
.
<td><%= link_to 'Show Email Addresses', product_senders_path(product) %> </td>
.
..
...
which seemed to be working and it redirected me to the senders of that product. Now for some strange reason I get this:
NameError in Senders#index
undefined local variable or method `sender_path' for #<#<Class:0x00000003cb1f58>:0x00000003b46e48>
Extracted source (around line #18):
15: <td><%= sender.product_id %></td>
16: <td><%= sender.name %></td>
17: <td><%= sender.email %></td>
18: <td><%= link_to 'Show', sender %></td>
19: <td><%= link_to 'Edit', edit_sender_path(sender) %></td>
20: <td><%= link_to 'Destroy', sender, confirm: 'Are you sure?', method: :delete %></td>
21: </tr>
This is my sender/index file:
<h1>Listing senders</h1>
<table>
<tr>
<th>Application</th>
<th>Name</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #senders.each do |sender| %>
<tr>
<td><%= sender.product_id %></td>
<td><%= sender.name %></td>
<td><%= sender.email %></td>
<td><%= link_to 'Show', sender %></td>
<td><%= link_to 'Edit', edit_sender_path(sender) %></td>
<td><%= link_to 'Destroy', sender, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
Why I am getting this error? Before it was working fine

As there are only nested routes for senders, there are no edit_sender_path(sender) helper, only edit_product_sender_path(product, sender)
You may see a list of all application route helpers by executing rake routes

Related

Error when delete comment Ruby on Rails

When I delete comment I see
undefined method 'each' for nil:NilClass
Also, the user cannot delete his account if has posted... It makes the error.
This is code
<p id="notice"><%= notice %></p>
<h1>Comments</h1>
<table>
<thead>
<tr>
<th>Link</th>
<th>Body</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #comments.each do |comment| %>
<tr>
<td><%= comment.link_id %></td>
<td><%= comment.body %></td>
<td><%= comment.user %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Comment', new_comment_path %>
Your are not checking if any comments are present before looping through them. So if there are no comments you are calling each on nil.
This should fix it
<tbody>
<% if #comments.any? %> <----- Add in this line and the end below
<% #comments.each do |comment| %>
<tr>
<td><%= comment.link_id %></td>
<td><%= comment.body %></td>
<td><%= comment.user %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %> <---- here
</tbody>

submit_tag with check_box_tag returning error

I'm having some trouble with check_box_tag and my method enable (a product can be disabled) in Ruby on Rails.
I created some checkbox so the user select the products that he wants to set enable = true.
Keeps returning the error "Couldn't find Product with 'id'=enable"
My method enable:
def enable
Product.update_all(["enable=?", true], :id => params[:selected_products])
redirect_to root_url
end
My routes.rb:
Rails.application.routes.draw do
get 'product_export/new'
get 'product_imports/new'
resources :users
resources :product_imports
resources :products do
collection do
post :import
post :export
post :enable
end
end
root to: 'products#index'
end
and finally my view with the table e the check_box_tag:
<%= form_tag enable_products_path, :method => :put do %>
<%= submit_tag "Enable products" %>
<table class="table table-striped table-responsive" id="productsTable">
<thead class="thead-inverse">
<tr>
<th/>
<th>Code</th>
<th>Enable</th>
<th>Bar code</th>
<th>Unit cost</th>
<th>Description</th>
<th>Family</th>
<th>Final</th>
<th>Measure</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag 'selected_products[]', product.id %></td>
<td><%= link_to (product.code), edit_product_path(product) %></td>
<td><%= product.enable %></td>
<td><%= product.bar_code %></td>
<td><%= product.unit_cost %></td>
<td><%= product.description %></td>
<td><%= product.family %></td>
<td><%= product.final %></td>
<td><%= product.measure %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
Thanks for the help!
Use where to find selected products and when to update them:
Product.where(id: params[:selected_products]).update_all(enable: true)

rails path helper doesnt work

Hello im learning Rails on my testing app and i have this code
<% #categories.each do |category| %>
<tr>
<td><%= category.name %></td>
<td><%= link_to 'Show', backend_category %></td>
<td><%= link_to 'Edit', edit_backend_categories(category) %></td>
<td><%= link_to 'Destroy', backend_category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
and Rake routes shows me this
home_index GET /home/index(.:format) home#index
root / home#index
contact /contact(.:format) home#contact
backend_root /backend(.:format) backend/admin#index
backend_categories GET /backend/categories(.:format) backend/categories#index
POST /backend/categories(.:format) backend/categories#create
new_backend_category GET /backend/categories/new(.:format) backend/categories#new
edit_backend_category GET /backend/categories/:id/edit(.:format) backend/categories#edit
backend_category GET /backend/categories/:id(.:format) backend/categories#show
PUT /backend/categories/:id(.:format) backend/categories#update
DELETE /backend/categories/:id(.:format) backend/categories#destroy
but im getting error that backend_category doesnt exist
here is image
http://www.nahraj-obrazek.cz/?di=213711395092
whats wrong ? Thank you
You must add _path to your link_to helper url, like so
<% #categories.each do |category| %>
<tr>
<td><%= category.name %></td>
<td><%= link_to 'Show', backend_category_path(category) %></td>
<td><%= link_to 'Edit', edit_backend_category_path(category) %></td>
<td><%= link_to 'Destroy', backend_category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Following Luis answer, any show path needs a reference to an object. So I believe it should be
<td><%= link_to 'Show', backend_category_path category %></td>
Which can also be written as:
<td><%= link_to 'Show', category %></td>

Forms broken after namespace modification

I recently modified my Sessions.rb controller as it was fighting against the Devise Sessions controller for supremacy in my application. It seems to have worked out well. I modified my route.rb as mentioned in the answer.
Now I'm having a couple of issues in the default (without much changes since the scaffold) sessions forms.
config/routes.rb
namespace :classroom do
resources :registrations
resources :sessions
end
classroom/sessions/index.html.erb
<h1>Listing sessions</h1>
<table>
<tr>
<th>Class size</th>
<th>Course</th>
<th>Description</th>
<th>Location</th>
<th>Name</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #sessions.each do |session| %>
<tr>
<td><%= session.class_size %></td>
<td><%= session.course_id %></td>
<td><%= session.description %></td>
<td><%= session.location_id %></td>
<td><%= session.name %></td>
<td><%= session.price %></td>
<td><%= link_to 'Show', session %></td>
<td><%= link_to 'Edit', edit_classroom_session_path(session) %></td>
<td><%= link_to 'Destroy', session, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Session', new_classroom_session_path %>
The following links has stopped working and gives a "Could not find a valid mapping for" error message when loading /sessions/index.html.erb
<td><%= link_to 'Show', session %></td>
td><%= link_to 'Edit', edit_classroom_session_path(session) %></td>
<td><%= link_to 'Destroy', session, method: :delete, data: { confirm: 'Are you sure?' } %></td>
Output of running rake routes:
classroom_sessions GET /classroom/sessions(.:format) classroom/sessions#index
POST /classroom/sessions(.:format) classroom/sessions#create
new_classroom_session GET /classroom/sessions/new(.:format) classroom/sessions#new
edit_classroom_session GET /classroom/sessions/:id/edit(.:format) classroom/sessions#edit
classroom_session GET /classroom/sessions/:id(.:format) classroom/sessions#show
PUT /classroom/sessions/:id(.:format) classroom/sessions#update
DELETE /classroom/sessions/:id(.:format) classroom/sessions#destroy
I'm not too sure how to modify the 'Show' and 'Destroy' part of the link to make them work with the new namespace. Thanks in advance for any help. Much, much appreciated.
Thanks,
Francis
try with:
<td><%= link_to 'Show', classroom_session_path(session) %></td>
<td><%= link_to 'Edit', edit_classroom_session_path(session) %></td>
<td><%= link_to 'Destroy', classroom_session_path(session), method: :delete, data: { confirm: 'Are you sure?' } %></td>
Look at here at a nice answer how to handle links with namespaced routes:
rails using link_to with namespaced routes
The name of the route is presented on the first column of the routes table. You should follow it on the link helper.
They should be:
<td><%= link_to 'Show', classroom_session_path(session) %></td>
<td><%= link_to 'Edit', edit_classroom_session_path(session) %></td>
<td><%= link_to 'Destroy', classroom_session_path(session), method: :delete, data: { confirm: 'Are you sure?' } %></td>

Rails 3.1 associations?

I have a Rails 3.1.1 application with the following models:
Company
Member
The two models have the following associations:
Company - has_many :members
Member - belongs_to :company
When adding members I can enter the company ID number and the record is linked successfully, I can lookup members through the company etc.
When I am working on the member show view I would like to 'pull' in details of the company.
Currently I have the following in the show view:
<h1>Listing members</h1>
<table>
<tr>
<th>Name</th>
<th>Mobile</th>
<th>Email</th>
<th>Qualifications</th>
<th>Membership</th>
<th>Company</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #members.each do |member| %>
<tr>
<td><%= member.name %></td>
<td><%= member.mobile %></td>
<td><%= member.email %></td>
<td><%= member.qualifications %></td>
<td><%= member.membership %></td>
<td><%= #member.company.company_id %></td>
<td><%= link_to 'Show', member %></td>
<td><%= link_to 'Edit', edit_member_path(member) %></td>
<td><%= link_to 'Destroy', member, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
How do I go about pulling in a field from the related company? For example the company model has two fields (latitude and longitude).
Your loop should look like this:
<% #members.each do |member| %>
<tr>
<td><%= member.name %></td>
<td><%= member.mobile %></td>
<td><%= member.email %></td>
<td><%= member.qualifications %></td>
<td><%= member.membership %></td>
<td><%= member.company_id %></td>
<td><%= member.company.latitude %></td>
<td><%= link_to member.company.name, member.company %></td>
<td><%= link_to 'Show', member %></td>
<td><%= link_to 'Edit', edit_member_path(member) %></td>
<td><%= link_to 'Destroy', member, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
company_id is defined on the Member model, you cannot access it through member.company.company_id. To access a field on the related company model, use member.company.my_field.
These will only work in the members loop, as they access the |member| variable which is passed to the block.
#member.company.latitude
et cetera. Please let me know if you want clarification or more information.

Resources