Can anyone tell me why the email addresses are not showing in the users list view below?
<p id="notice"><%= notice %></p>
<h1>Users</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
**<td><%= user.email %></td>**
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
I'm not familiar with seeing the two asterisks around the element. I'm thinking that might be messing something up. If you want to bold the user's email you could try something like <td><b><%= user.email %></b></td>.
Are you getting any errors anywhere?
If the asterisks are just there to help us see the line of code, and you say that you see the users (and their emails) in the db, I think it could be one of two other relatively minor things.
Whats your controller look like for this view? Are you passing #users to this view? You could try just dumping #users on the page to make sure that it's available.
Is the field on your User model that holds the email called email?
Related
I want this function, if the status of progress so that it changes on the done exactly at those IDs where the status is for the conditions, at the moment I get an error and secondly, if it worked without errors, then it would change everything, but I need if progress is finished and if you don’t, then on progress
I selected three records through the form using a checkbox ...
in the function, I want that if the status of the record from database is "done", then it should change to "progress", if the status is "progress" in the record, then it should be changed to "done". Now I click on "submit_tag" and I get an error and in any case this code will not work the way I want it, I want everything to be conditional.
I am completely in ruby, help please, maybe the problem is in the syntax
no implicit conversion from nil to integer
def update_me
#iteam = Iteam.find(params[:id])
if #iteam[params[:status]] == 'progress'
Iteam.where(params[:id]).update_all(status: 'DONE')
else
Iteam.where(params[:id]).update_all(status: 'progress')
end
end
index view
<%= form_tag update_me_iteams_path, :method =>'put' do %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th>Status</th>
<th></th>
</tr>
<% #iteams.each do |iteam| %>
<tr>
<td><%= iteam.id %></td>
<td><%= iteam.title %></td>
<td><%= iteam.text %></td>
<td><%= iteam.status %></td>
<td><%= link_to 'Show', iteam_path(iteam) %></td>
<td>
<%= check_box_tag "id[]", iteam.id %>
</td>
</tr>
<% end %>
</table>
<%= submit_tag "Edit Checked" %>
<% end %>
Please Use following to find with id:
Iteam.where(id: params[:id])
or you can use the following
Iteam.find(params[:id])
I just created a scaffold in rails and list all stories in index page, and _stories.html.erb is partial which was rendered in index.html.erb
I want each story div has a red background for example:
.storyCell{
background-color:red;
height:100px;
}
_stories.html.erb
<tbody>
<% #stories.each do |story| %>
<div class="storyCell">
<tr>
<td><%= story.content %></td>
<td><%= story.finished %></td>
<td><%= link_to 'Show', story %></td>
<td><%= link_to 'Edit', edit_story_path(story) %></td>
<td><%= link_to 'Destroy', story, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</div>
<% end %>
</tbody>
But the result is the red div all in top of the story model properties. Thank you!
This is invalid html. You can't have a div inside a tbody. Remove your div, and just put the class directly on the table row:
<tr class="storyCell">
What's happening here is that the browser is trying to do the best it can to render the invalid html, and so it pulls the div out (which it nows is not allowed inside the table) and renders it above the table instead.
You can give the class to <tbody> like this:
<tbody class="storyCell">
Here is the w3schools example you can look at: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_tbody
.You can't insert div in table elements as it will render in correct html output.
I generated a scaffold for a To-Do List app, and I left out some columns to add later.
I ran the command to create a migration to add a new column named client and I changed my files so that it shows on the projects index and form, but when i enter something into the client field and submit, it doesn't save the information and remains blank..
Update 1:
Here is whats in my routes:
'
Rails.application.routes.draw do
root :to => 'projects#index'
resources :projects
end
'
Here is my index view:
'
<h1 id="title">Project List</h1>
<table>
<thead>
<tr id="headers">
<th>Title</th>
<th>Client</th>
<th>Description</th>
<th>Hours</th>
<th>Done</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody class="col-md-2" id="listItems">
<% #projects.each do |project| %>
<tr id="table">
<td><%= project.title %></td>
<td><%= project.client %></td>
<td><%= project.description %></td>
<td><%= project.hours %></td>
<td><%= project.done %></td>
<td><%= link_to " #{image_tag('show.png')}".html_safe, project, id:'showButton' %></td>
<td><%= link_to " #{image_tag('edit.png')}".html_safe, edit_project_path(project), id:'editButton' %></td>
<td><%= link_to " #{image_tag('destroy.png')}".html_safe, project, id:'destroyButton', method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Project', new_project_path, id:"new" %>
<footer id="footer">Copyright 2014 Kira Banks</footer>
'
To keep your application secured, Rails has a feature called Strong Parameters and the docs says:
It provides an interface for protecting attributes from end-user
assignment. This makes Action Controller parameters forbidden to be
used in Active Model mass assignment until they have been whitelisted.
So, basically you need to whitelist the new client attribute in the Projects controller by adding it to the list:
class ProjectsController < ApplicationController
# ...
# at the end of the file
private
def project_params
params.require(:project).permit(:title, :description, :hours, :done, :client)
end
end
This question already has answers here:
Destroying users as an admin in Devise
(3 answers)
Closed 8 years ago.
I am trying to have an admin user delete other users. When i try to delete another users registration path it deletes the admin. Ive seen a few other posts with the same issue but have not been able to replicate it.
This is all_users.html.erb :
<h2 class="admin">All Users</h2>
<table class="table table-striped">
<tbody>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Cell Phone</th>
<th>Login Method</th>
<th>Delete User</th>
</tr>
</thead>
<tr>
<% #users.each do |user| %>
<td><%= user.first_name %></td>
<td><%= user.last_name %></td>
<td><%= user.cell_phone %></td>
<td><%= user.sprovider %></td>
<td><%= link_to user_registration_path, data: {confirm: "Are you sure?"}, method: :delete do%>
<span class="fa fa-trash-o"></span>
<% end %></td>
</tr>
<% end %>
</tbody>
</table>
Why is the current_user (the admin) being deleted instead of the user who i am calling the delete method on?
Looks like you're using Devise as the auth method for your users. Although devise will create routes for authentication / registration (and even a self account descruction) it does not create the regular REST controller for the User model.
I would suggest to create a new UsersController, with the regular index, show, delete, update, create, etc., as well as the regular route
resources :users
and use that controller for deleting other users. That way you could have a proper
<%= link_to #user, method: :delete, confirm: 'Are You Sure?' %>
How can I return data from the database on another page?
I can file this under: views / posts / index.htm.erb
<h1>Listing posts</h1>
<table>
<tr>
<th>Titulo</th>
<th>Conteudo</th>
<th>Categoria</th>
<th>Criado em</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.titulo %></td>
<td><%= post.conteudo %></td>
<td><%= post.category.name %></td>
<td><%= post.created_at.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Post', new_post_path %>
And I wanted to display these values on another page: views / home / blog.html.erb
How do I do this? Could you explain all the steps so that I can display these values in my other page.
Thanks Kocur4d
but how do I get only some information? eg: I would like that to appear in the title of the post page like this way (in blog.html.erb):
<div class="post-title">
<h2 class="title"> <a href="link_to_post"> **<% = post.titulo%>** </ a> </ h2>
</ div>
Step 1. Create controller
In your app root directory run:
rails g controller home blog
Modify controllers/homes_controller.rb :
class HomesController < ApplicationController
def blog
#posts = Post.all
end
end
Your controllers/posts_controller.rb should be already set up. Minimum what you need for your question is to have index method defined you might have other methods as well:
class PostsController < ApplicationController
def index
#posts = Post.all
end
end
Step 2. Extract Partial
change views/posts/index.htm.erb :
<h1>Listing posts</h1>
<%= render partial: 'shared/posts', object: #posts %>
<%= link_to 'New Post', new_post_path %>
create/modify views/home/blog.html.erb :
<h1>Listing posts</h1>
<%= render partial: 'shared/posts', object: #posts %>
<%= link_to 'New Post', new_post_path %>
create views/shared/_posts.html.erb :
<table>
<tr>
<th>Titulo</th>
<th>Conteudo</th>
<th>Categoria</th>
<th>Criado em</th>
<th></th>
<th></th>
<th></th>
</tr>
<% posts.each do |post| %>
<tr>
<td><%= post.titulo %></td>
<td><%= post.conteudo %></td>
<td><%= post.category.name %></td>
<td><%= post.created_at.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
Step 3. Set up routes.
You should have something like this in your routes.rb file:
resources :posts or match 'posts/index' => 'posts#index'
add this to config/routes.rb:
match 'home/blog' => 'home#blog'
so it might look like this(there is few variants):
config/routes.rb:
YourAppName::Application.routes.draw do
root to: 'posts#index'
resources :posts
match 'home/blog' => 'home#blog'
end
Now when you start rails server(assuming standard configuration) and visit:
127.0.0.1:3000/posts/index and 127.0.0.1:3000/home/blog
you should see same content.
This should work copy-and-paste but I could make some typos and other small mistakes(hope not, if ill find any ill try to edit them asap). In general look at it as you need 3 steps to forward http request down your rails application stack.
Map url to controllers using routes.
Create controllers and inside prepare data for views.
Display data in Views.
Look around in Rails Guides, Rails for Zombies and Rails Tutorial for more info.
---------Upadate to your second question-----------
I don't really understand what would you like to achieve?? At the moment both index.html.erb and blog.html.erb showing the same data, that was what you ware asking for?
post representing one post and is available in sharde/_posts.html.erb. You can't reference it from index.html.erb or blog.html.erb.
#posts represents all the posts and its available in index.html.erb or blog.html.erb.
render partial: 'shared/posts', object: #posts -- this line say "Hey man! Paste here content of shared/posts file, and btw I have here a local variable #posts so if you need to use that date in shared/posts file Ill name it posts from in side there"
To make them look different modify both files and part that will be identical for both of them is in a sharde/_posts.html.erb.
Try for example remove this line:
<td><%= post.category.name %></td>
from shared file to see what is going to happen.
Add some html tags and thinker with it.
Rails has may helper methods available' to find out about them check the links I give you and google, google, google.
Try to add some links with link_to helper
In your home controller, for the blog method, set #posts as you need...
Maybe
#posts = Post.all