link_to Show page takes me to the index page Rails? - ruby-on-rails

I am trying to have a dashboard in my app that lists all the reviews the user has. The problem that i have is when i click on the post that the user gave a review in , it takes me to the index page of all the posts instead of the show page of the specific post. This is the line of code i am having issue with <td><%= link_to review.post.title , posts_path(#post) %></td>
. Here's my code:
views/pages/dashboard.html.erb
<div class="align-left">
<div class="col-md-2">
<h5><%= #user.name %></h5>
</div>
<div class="col-md-5">
<h3>My Posts</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= time_ago_in_words(post.created_at) %> ago</td>
<td><%= link_to "Edit", edit_post_path(post) %>|<%= link_to "Destroy", post_path(post), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
<h3>My Reviews</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Place</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
<% #reviews.each do |review| %>
<tr>
<td><%= link_to review.post.title , posts_path(#post) %></td>
<td><%= time_ago_in_words(review.created_at) %> ago</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
the Rake route file

There is a instance variable #posts not #post on dashboard.html.erb page.
And you can get post by review.post, like:-
<td><%= link_to review.post.title , post_path(review.post) %></td>
Also, Instead of call association again and again you can do this:-
<% #reviews.each do |review| %>
<tr>
<% review_post = review.post %>
<td><%= link_to review_post.title , post_path(review_post) %></td>
<td><%= time_ago_in_words(review.created_at) %> ago</td>
</tr>
<% end %>

Should be <%= link_to review.post.title, post_path(#post) %>,
or just <%= link_to review.post.title, #post %>
not <%= link_to review.post.title, posts_path(#post) %>.

You have not check your routes properly:
posts_path is for index method and for show method it is post_path with id or object.
Use:
<td><%= link_to review.post.title , post_path(#post) %></td>

Related

How to make a nested table in rails

I want to create a table named groups that lists of each group. Inside of each group, I want to list the groups courses. The group has_many :courses, and the course belongs_to :group. My current attempt does not work, and I have no idea where to go from there
Current Attempt
courses_controller.rb
def index
#courses = Course.find_by(group: :group_id)
#groups = Group.all
end
index.html.erb
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
<td><%= link_to 'Show', group %></td>
<td><%= link_to 'Edit', edit_group_path(group) %></td>
<td><%= link_to 'Destroy', group, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>
<% if current_user.master? %>
<td><%= link_to 'Edit', edit_course_path(course) %></td>
<td><%= link_to 'Destroy', course, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if current_user.master? %>
<%= link_to 'New Course', new_course_path %>
<% end %></td>
</tr>
<% end %>
</tbody>
</table>
Any help is appreciated :)
UPDATE
:group_id is the groups id for each created course, since the group has_many :courses The lesson belongs_to :course but that isnt important, as I just want the table, Thanks XP
As I said in my comment, what do you mean it isn't working? Are you getting any error messages? How have you structured you models? Have you created a migration to add group id to courses? What does your routes file look like - are courses nested under groups? If you don't provide this kind of information, its difficult to provide help.
If you look in the logs, you'll get some messages which will direct you to where things are going wrong. Have you tried running
Course.find_by(group: :group_id)
in a rails console. I suspect you'll find that this line is causing problems. Comment out this line and amend your erb as follows (I've not included all the code, but just to give you an idea). Also its not a good idea to use tables within tables.
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #group.courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>

What is error with cancan in view

I am using cancan to authorize the user in controller,And in views i am using Can?to check if the user is authorize to see that function
index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Noticeboards</h1>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>society</th>
<th>Notice heading</th>
<th>Notice text</th>
<th>Active</th>
<th>Isdelete</th>
<th>Expire</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #noticeboards = #noticeboards.where(Isdelete: 0).find_each %>
<% #noticeboards.each do |noticeboard| %>
<tr>
<td><%= Society.find_by(id: noticeboard.id_society, Isdelete: 0).name %></td>
<td><%= noticeboard.notice_heading %></td>
<td><%= noticeboard.notice_text %></td>
<td><%= noticeboard.active %></td>
<td><%= noticeboard.IsDelete %></td>
<td><%= noticeboard.Expire %></td>
<td><%= link_to 'Show', noticeboard %></td>
<% if can? :update, noticeboard %>
<td><%= link_to 'Edit', edit_noticeboard_path(noticeboard) %> <% end %></td>
<% if can? :delete , noticeboard %>
<td>
<%= link_to 'Destroy', noticeboard, method: :delete, data: { confirm: 'Are you sure?' } %></td> <% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if can? :create, #noticeboard %>
<%= link_to 'New Noticeboard', new_noticeboard_path %>
<% end %>
In this if i change :delete to anything else, say :Hello, its still checking can, and if i am not using anything, it gives error saying "wrong number of arguments" what is the error, why its not taking :delete function?

param is missing or the value is empty: record

Hi I am using Ruby on Rails to show an table, this is the code in view:
<h1> Show by category</h1>
<h2> table </h2>
<table border="1" style="width:70%">
<tr>
<th>Name</th>
<th>SKU</th>
<th>Category</th>
<th><%= link_to "New",new_record_path %>
</tr>
<%= #records.each do |r| %>
<tr>
<td><%= r.name %></td>
<td><%= r.sku %></td>
<td><%= r.category %></td>
<td><%= link_to "Edit", edit_record_path(r) %></td>
</tr>
<% end %>
<%= will_paginate #records %>
</table>
and I got this
http://i.stack.imgur.com/rUN0v.png
Between the headline and the table, the records are all shown up, I don't know where it is coming from, can somebody help?
The problem is here in this line
<%= #records.each do |r| %>
which should be
<% #records.each do |r| %>
Imp note:
<% %> #Executes the statement/expression.
<%= %> #Prints the output.

Undefined method Error message in Ruby 2.1

I have zero experience with Ruby, and I'm having the following issue:
I receive a "We're sorry, but something went wrong" error message when I try to login into my admin panel (mydomain.com/administrator).
Checking the logs I found out the following:
As well, I checked the login_controller.rb:
Here you have the template:
<h1><%= t(".title")%></h1>
<!-- Pages -->
<% if #pages.length > 0 %>
<h3><%= t(".lastets_pages")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.page.title")%></th>
<th><%=t("activerecord.attributes.page.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #pages.each do |page| %>
<tr>
<td><%= page.id %></td>
<td><%= page.title %></td>
<td><%= page.slug %></td>
<td><%= page.user.username %></td>
<td><%= l(page.created_at, format: :long) %></td>
<td><%= l(page.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_page_path(page) %>
<%= link_to t("generic.delete"), administrator_page_path(page), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_pages")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_pages_empty")%> </div>
<% end %>
<!-- Notices -->
<% if #notices.length > 0 %>
<h3><%= t(".lastets_notices")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.notice.title")%></th>
<th><%=t("activerecord.attributes.notice.countries")%></th>
<th><%=t("activerecord.attributes.notice.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #notices.each do |notice| %>
<tr>
<td><%= notice.id %></td>
<td><%= notice.title %></td>
<td><%= notice.show_countries %></td>
<td><%= notice.slug %></td>
<td><%= notice.user.username %></td>
<td><%= l(notice.created_at, format: :long) %></td>
<td><%= l(notice.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_notice_path(notice) %>
<%= link_to t("generic.delete"), administrator_notice_path(notice), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_notices")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_notices_empty")%> </div>
<% end %>
<!-- Faqs -->
<% if #faqs.length > 0 %>
<h3><%= t(".lastets_faqs")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.faq.title")%></th>
<th><%=t("activerecord.attributes.faq.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #faqs.each do |faq| %>
<tr>
<td><%= faq.id %></td>
<td><%= faq.title %></td>
<td><%= faq.slug %></td>
<td><%= faq.user.username %></td>
<td><%= l(faq.created_at, format: :long) %></td>
<td><%= l(faq.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_faq_path(faq) %>
<%= link_to t("generic.delete"), administrator_faq_path(faq), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_faqs")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_faqs_empty")%> </div>
<% end %>
Notice.rb model:
Can you help me to identify the issue? I tried to clear the cache and nothing happened.
The error backtrace points to the notice.rb:7 file, which is the line inside the block:
countries << country_post.country.name
Undefined method 'name' for nil:NilClass means you are trying to call nil.name, so for a given country_post, country_post.country returns nil.
You'll have to check this part of the code to solve the bug. Another thing you can do is use the try method:
countries << country_post.country.try(:name)
this try method will return country.name if country is not nil, ornil` otherwise:
https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L93
https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L62
In your Notice.rb model use:
countries << country_post.country.try(:name)
It seems that you are trying to use the notice.show_countries method.
The log is telling you that the method name does not exist for nil, which means that your object is nil.
Please show us the content of the "show_countries" method and please check that you have an acutal object given to the line of code responsible for the method name call.
Have you tried using a debugger ?
UPDATE:
Based on the notice code, your problem is that some post, does not have a coutry, then, country_post.country is nil which result to your error.
Either you ensure that you have a default country at the creation, or you do it in two step when trying to retrieve the information
if country_post.country.blank?
countries << 'DEFAULT_COUNTRY_NAME'
else
country << country_post.country.name
The answer by #mrcasals most directly answers the question and will result in that exception no longer being raised.
However, a more "fundamental" problem here is that you are expecting some data to be persisted to the database which is not. Rails' canonical solution to the problem of ensuring the existence of attributes is via ActiveRecord validations. The general overview is a great read, and the presence validation should do the trick for this problem in particular.

Rails form to update a record within an index partial

Hi can anyone tell me what I'm doing wrong here. I'm trying to allow the user update certain fields from within the index. However, when I press the button on the screen to update, nothing happens and the record in the database remains unchanged. What am I missing or doing wrong?
views/company_pays/_index.html.erb
<h1>Company Pays</h1>
<table class="table">
<thead>
<tr>
<th>Description</th>
<th>Type</th>
<th>Units</th>
<th>Apply deduction1</th>
<th>Apply deduction2</th>
<th>Apply deduction3</th>
<th>Apply deduction4</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #company_pays.each do |company_pay| %>
<tr>
<td><%= company_pay.description %></td>
<td><%= company_pay.pay_sub_head.short_desc %></td>
<td class="input"><%= form_for company_pay, :method => :patch do |f| %></td>
<% if company_pay.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(company_pay.errors.count, "error") %> prohibited this company_pay from being saved:</h2>
<ul>
<% company_pay.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<td class="input"><%= f.text_field :units %></td>
<td class="input"><%= f.check_box :apply_deduction1 %></td>
<td class="input"><%= f.check_box :apply_deduction2 %></td>
<td class="input"><%= f.check_box :apply_deduction3 %></td>
<td class="input"><%= f.check_box :apply_deduction4 %></td>
<td class="actions"><%= f.submit "Update" %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
Thanks for looking

Resources