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.
Related
I get this error: app/views/topics/show.html.erb:49: syntax error, unexpected keyword_ensure, expecting keyword_end. I don't have a line 49. I checked to see if I was missing any <% end %> and I find that I don't.
<h1><%= #topic.name %></h1>
<% if user_is_authorized_for_topics? || if user.moderator? %>
<%= link_to "Edit Topic", edit_topic_path, class: 'btn btn-success' %>
<% end %>
<% if user_is_authorized_for_topics?%>
<%= link_to "Delete Topic", #topic, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>
<% end %>
<div class="row">
<div class="col-md-8">
<p class="lead"><%= #topic.description %></p>
<% #topic.posts.each do |post| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<%= link_to post.title, topic_post_path(#topic, post) %>
</h4>
<small>
submitted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.name %> <br>
<%= post.comments.count %> Comments
</small>
</div>
</div>
<% end %>
<% #topic.sponsored_posts.each do |sponsored_post| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<%= link_to sponsored_post.title, topic_sponsored_post_path(#topic, sponsored_post) %>
</h4>
<small>
submitted <%= time_ago_in_words(sponsored_post.created_at) %> ago <br>
</small>
</div>
</div>
<% end %>
</div>
<% if current_user %>
<div class="col-md-4">
<%= link_to "New Post", new_topic_post_path(#topic), class: 'btn btn-success' %>
</div>
<% end %>
</div>
Probably this line:
<% if user_is_authorized_for_topics? || if user.moderator? %>
You only need if once, like this:
<% if user_is_authorized_for_topics? || user.moderator? %>
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 .
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 %>
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| %>
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 %>