Combining Multiple Models into one view Rails - ruby-on-rails

I want to show data from 4 models into a single view. I have created a separate controller for this named posts_controller.rb and in views I have created posts folder and created index.html.erb file.
After that in controllers file I have added the following code.
class PostsController < ApplicationController
def index
#quotes = Quote.all.order("created_at DESC")
#images = Image.all.order("created_at DESC")
#jokes = Joke.all.order("created_at DESC")
#items = (#quotes.to_a + #jokes.to_a)
end
end
And here is the view file where I am trying to show 2 items data as for now. But its not working. Pls check the code.
<% if #items.any? %>
<div class="col-md-12">
<% #items.each.do |item| %>
<% if item.is_a? Quote %>
<div class="postbg">
<%= quote.quotefie %>
<div class="wedate pull-right wehi">
<%= quote.created_at.strftime("%b %d, %Y") %>
</div>
<div class="clearfix"></div>
<em class="pull-right wehi" style="margin-top:20px;"> - <%= quote.author %></em>
<%= link_to 'Show', quote %>
<%= link_to 'Edit', edit_quote_path(quote) %>
<%= link_to 'Destroy', quote, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% else %>
<% #jokes.each do |joke| %>
<div class="postbg">
<%= joke.jokefie %>
<div class="wedate pull-right wehi">
<%= joke.created_at.strftime("%b %d, %Y") %>
</div>
<div class="clearfix"></div>
<%= link_to 'Show', joke %>
<%= link_to 'Edit', edit_joke_path(joke) %>
<%= link_to 'Destroy', joke, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
<% end %>
</div>
<% end %>
Here is the error it is showing.
undefined method `do' for #<Enumerator:0x007fd55fb032b8>

Your problem is in this line here:
<% #items.each.do |item| %>
do defines a block, not a method call, so it should be:
<% #items.each do |item| %>
Note the removed ..
You've used do correctly further down the view when you use #jokes.each do |joke|.

This line
<% #items.each.do |item| %>
should be
<% #items.each do |item| %>

Related

Rails 6 with ancestry gem: show entire tree, from root until the very last subcategory

I'm using the ancestry gem in order to create categories, subcategories, sub-subcategories and so on. Everything is working just fine, I can create a category, then create a subcategory which belongs to this category and then create a sub-subcategory for the subcategory and so on. The only issue I'm having is that I cant figure out how to show all the categories with each full tree from root until the very last subcategory inside a view.
Currently I have the following setup and I'm getting only up to the subcategory:
Inside the controller: #categories = Category.where(ancestry: nil).order('name ASC')
Inside the view :
<% #categories.in_groups_of(3) do |category| %>
<div class="row">
<% category.each do |item| %>
<div class="col-md-4">
<% if item.present? %>
<ul>
<li><%= (item.name) %>
<%= link_to edit_category_path(item) do %><i class="fas fa-edit"></i><% end %>
<%= link_to new_subcategory_path(ancestry: item) do %><i class="fas fa-plus-circle"></i><% end %>
<%= link_to category_path(item), :method => :delete, data: { confirm: "Are you sure?" } do %><i class="far fa-trash-alt"></i><% end %>
<% unless item.children.empty? %>
<ul>
<% item.children.sort_by(&:name).each do |subcategory| %>
<li>
<%= subcategory.name %>
<%= link_to edit_category_path(subcategory) do %><i class="fas fa-edit"></i><% end %>
<%= link_to new_subcategory_path(ancestry: subcategory) do %><i class="fas fa-plus-circle"></i><% end %>
<%= link_to category_path(subcategory), :method => :delete, data: { confirm: "Are you sure?" } do %><i class="far fa-trash-alt"></i><% end %>
</li>
<% end %>
</ul>
<% end %>
</li>
</ul>
<% end %>
</div>
<% end %>
</div>
<% end %>
Then I found this solution, which does show the root until the very last subcategory. But I cant really use this solution, since I have a few link_to that I want to show inside the loop(as you can see above). Any ideas how I can make the above solution show the root until the very last subcategory?
#items_tree = Category.all.arrange
def nested_groups(groups)
s = content_tag(:ul) do
groups.map do |group, sub_groups|
content_tag(:li, (group.name + nested_groups(sub_groups)).html_safe)
end.join.html_safe
end
end

How do I output the contents of a variable in grid form?

I have the following code for my Ruby on Rails project, which outputs a list of users on a page:
<ul class="users">
<%= render #users %>
</ul>`
The result is a long list of users on the page.
My question is: How can I manipulate #users so that I can output its content in grid form (e.g., a row of 5 users and then next row and then next and so on) on the page? Thanks.
This is a really simple example and you can customise into a nicer table.
The basic idea is to group the array of users in subarrays of n elements, lets say 5.
<% #users.each_slice(5).each do |row| %>
<p><% row.each do |user| %>
<%= user.name %> |
<% end %>
</p>
<% end %>
<div class="row">
<% users.in_groups_of(5) do |group| %>
<% group.compact.each do |user| %>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<%= image_tag (user.file_url), class: "img-sz" %>
<h4><%= link_to user.title, user %></h4>
<%= link_to "Edit", edit_user_path(user) , remote: true, class: "btn btn-default btn-xs" %>
<%= link_to "delete", user,remote: true, method: :delete, data: { confirm: "You sure?" }, class: "btn btn-danger btn-xs" %></br></br>
</div>
<% end %>
<% end %>
<% end %>
</div>
This is example code change the name of attributes according to your structure . This will help you in solving your problem .

I can't find the error and it's driving me crazy

This is my index.html.erb for Post#index
I get this error:
/home/ubuntu/workspace/app/views/posts/index.html.erb:34: syntax error, unexpected '\n', expecting &. or :: or '[' or '.'
please help!
<div class="title">
<h1>All Posts</h1>
</div>
<div id="post_list">
<% for #posts.each do |p| %>
<div class="post_title">
<h2><%= p.date.strftime("%Y%m%d") %></h2>
</div>
<div class="post_type">
<h3><%= p.type %></h3>
</div>
<div class="post_content">
<p><%= p.content %></p>
</div>
<% unless p.is_cited='false' %>
<div class="post_citation">
<p><i> <%= p.citation %></i></p>
</div>
<% end %>
</br>
<div class="button">
<div class="action_buttons">
<div class="show">
<%= link_to 'Show', #post %>
</div>
<div class="edit">
<%= link_to 'Edit', edit_post_path %>
</div>
<div class="delete">
<%= link_to 'Delete', post_path(#post), data: {confirm: "Are you sure?"}, :method => :delete %>
</div>
</div>
</div>
<% end %>
</div>
Instead of the following:
<% for #posts.each do |p| %>
try:
<% #posts.each do |p| %>
Not sure if this is what's causing the problem, but you're referencing #post inside your each block. Try changing
<%= link_to 'Show', #post %>
to
<%= link_to 'Show', p %>
and change
<%= link_to 'Delete', post_path(#post), data: {confirm: "Are you sure?"}, :method => :delete %>
to
<%= link_to 'Delete', post_path(p), data: {confirm: "Are you sure?"}, :method => :delete %>

How can I add a comment list in my posts' index.html page?

How can I add a comment list in my posts' index.html page?
It is my PostsController:
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
#comments = #post.comments.all
#comment = #post.comments.build
end
and its my posts show view:
<p id="notice"><%= notice %></p>
<p>
<h3><%= #post.name %></h3>
</p>
<p>
<%= (#post.descriptopm).html_safe %>
</p>
<%= link_to 'Edit', edit_post_path(#post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Back', posts_path, :class => "btn btn-info btn-xs" %>
<h3>Comments</h3>
<% #comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
and its my posts index view:
<h1>Listing posts</h1>
<%= link_to 'Create a New Post', new_post_path, :class => "btn btn-success btn-sm" %>
<% #posts.each do |post| %>
<div class="post thumbnail">
<h3><%= post.name %></h3>
<div><%= (post.descriptopm).html_safe %></div>
<div class="bottom-bottoms">
<%= link_to 'Display', post, :class => "btn btn-info btn-xs" %>
<%= link_to 'Edit', edit_post_path(post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-info btn-xs" %>
</div>
<h3>Comments</h3>
<% #post.comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
</div>
<% end %>
the post.rb :
class Post < ActiveRecord::Base
has_many :comments
end
the comment.rb :
class Comment < ActiveRecord::Base
belongs_to :post
end
the show page show the comments well but the index cannot...
it shows the error : undefined method `comments' for #
please help me >"<
I am a newly Ruby on Rails writer
Associations
If you're using ActiveRecord association to "link" your Post and Comment model, you'll be able to call .comments on each post you have in your index
Something to note (and this lies at the core of your error) is that you will have to call the .comments method on each instance of the Post. You're currently trying to call it on an #instance variable which doesn't exist:
#app/views/posts/index.html.erb
<% #posts.each do |post| %>
<% post.comments.each do |comment| %>
<%= comment.body %>
<% end %>
<% end %>
This, of course, is only possible by using the setup you have already (has_many / belongs_to):
#app/models/post.rb
Class Post < ActiveRecord::Base
has_many :comments #-> post.comments
end
#app/models/comment.rb
Class Comment < ActiveRecord::Base
belongs_to :post #-> comment.post
end
instead of using #post in comment use post on index page. You have typo error.
<%= link_to 'Create a New Post', new_post_path, :class => "btn btn-success btn-sm" %>
<% #posts.each do |post| %>
<div class="post thumbnail">
<h3><%= post.name %></h3>
<div><%= (post.descriptopm).html_safe %></div>
<div class="bottom-bottoms">
<%= link_to 'Display', post, :class => "btn btn-info btn-xs" %>
<%= link_to 'Edit', edit_post_path(post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-info btn-xs" %>
</div>
<h3>Comments</h3>
<% post.comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
</div>
<% end %>

Only Admin Role Can Edit or Delete Notes Rails 4

I am having a simple issue but nonetheless unable to figure it out. My employee role can not edit or delete notes but they can view them & they can also create them.
My admin role however can view, create, edit & delete. My employee section is working but my admin is not. I am still new to rails so please bear with my ignorance.
My error is "NameError in Notes#index" and "undefined local variable or method `note' for #<#:0x007fa7b580c0a0>"
In my notes/index.html.erb file my code is as follows:
<% if #user.role == "employee" %>
<%= link_to 'New Note', new_note_path, class: "btn btn-primary btn-lg" %>
<% #notes.each do |note| %>
<div class="row notes-row"><h1><%= note.title %></h1></div>
<div class="row notes-row"><h3>Created By: </h3><%= note.user_name %></div>
<div class="row notes-row"><h6>Posted On:</h6><%=note.created_at%></div>
<div class="row notes-row"><%= note.text_box %></div>
<br>
<p><%= link_to 'View', note %></p>
<br>
<hr>
<% end %>
<% elsif #user.role == "admin" %>
<%= link_to 'Add New Job', new_job_path, class: "btn btn-primary btn-lg" %>
<%= link_to 'Edit', edit_note_path(note) %><%= link_to 'Destroy', note, method: :delete, data: { confirm: 'Are you sure?' } %>
<br>
<p> <% #notes.each do |note| %> | </p>
<%= link_to 'View', note%>
<%end%>
<% end%>
My index method is :
def index
#users = User.all
#user = current_user
#notes = Note.all
#notes = Note.paginate(:page => params[:page],:per_page => 5)
end
Your error arises from this point;
<%= link_to 'Edit', edit_note_path(note) %><%= link_to 'Destroy', note, method: :delete, data: { confirm: 'Are you sure?' } %>
The variable note is unknown at this point.
You could re-factor it this way;
<% if #user.role == "employee" %>
<%= link_to 'New Note', new_note_path, class: "btn btn-primary btn-lg" %>
<% end %>
<% #notes.each do |note| %>
<% if #user.role == "employee" %>
<div class="row notes-row"><h1><%= note.title %></h1></div>
<div class="row notes-row"><h3>Created By: </h3><%= note.user_name %></div>
<div class="row notes-row"><h6>Posted On:</h6><%=note.created_at%></div>
<div class="row notes-row"><%= note.text_box %></div>
<br>
<p><%= link_to 'View', note %></p>
<br>
<hr>
<% end %>
<% if #user.role == "admin" %>
<%= link_to 'Add New Job', new_job_path, class: "btn btn-primary btn-lg" %>
<%= link_to 'Edit', edit_note_path(note) %><%= link_to 'Destroy', note, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<% end %>
<br>
<% if #user.role == "admin" %>
<p> <% #notes.each do |note| %> | </p>
<%= link_to 'View', note%>
<%end%>
<% end %>
The code can be cleaned up further.

Resources