I want to model student's attendance, i am following railscast.com/165-edit-multiple-revised i follow Ryan's first approach and try to use it with mongoid. My modified code is as follows but it does not update multple records, well it even doesnt update any of the record. sounds like mongoid does not found any Student with id of an array. plz see the code and let me know what i am doing wrong.
# config/routes.rb
resources :students do
collection do
put :attendance
end
end
# model/student.rb
class Student
include Mongoid::Document
field :name, type: String
field :present, type: Mongoid::Boolean
end
# controllers/students_controller.rb
def attendance
Student.where(id: params[:student_ids]).update_all(present: true)
redirect_to students_url
end
# views/students/index.html.erb
<% #students.each do |student| %>
<tr>
<td><%= check_box_tag "student_ids[]" , student.id %></td>
<td><%= student.name %></td>
<td><%= student.present %></td>
<td><%= link_to 'Show', student %></td>
<td><%= link_to 'Edit', edit_student_path(student) %></td>
<td><%= link_to 'Destroy', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
if i change the code in controller as follows than it works but i am not satisfied with this code. is there any other way around or i am doing something wrong?
# controllers/students_controller.rb
def attendance
params[:student_ids].each do |student_id|
Student.where(id: student_id).update_all(present: true)
end
redirect_to students_url
end
If you want to fetch a set of student, you should try the in operator. Try this:
def attendance
Student.where(:id.in => params[:student_ids]).update_all(present: true)
redirect_to students_url
end
What do you think?
Related
I am using Devise gem for user Authentication. User id is as foreign key in article table
How can i get writer name through User_id in a view Show_article.html.erb
I can access user_id in show_article.htmlerb
I have tried to make a custom function in article controller but could not get the desired output
your Article model should look like this:
class Article << ActiveRecord::Base
belongs_to :user
#....some more lines
end
your User model should look like this:
class User << ActiveRecord::Base
has_many :articles
#....some more lines
end
and your show.html.erb should say:
#....your rest of code
<%= #article.try(:user).try(:name) %>
#....your rest of code
This will skip user name if you article doesn't have any user. It looks like your article doesn't have user_id or you haven't defined your relations correctly.
It might be the case that some of your article do not have any user. so
you can do
<%= #article&.user&.name %> in show page
and in the index page
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article&.user&.name%></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Delete', article_path(article),
method: :delete,
data: { confirm: 'Are you sure you want to delete this article?' } %></td>
</tr>
<% end %>
but make sure you have ruby 2.3.0 or higher
I have been try examples the past couple of days with no luck.
I have a table named Article with columns such as bullet, powder, and calibertitle. I set up a form in which I can enter in particular details such as bullet brand, powder brand, caliber name, etc.
I am using SQLite.
How can I search for a particular word in a title and only display the title and corresponding info if the title has that word?
I can list all data but I am not sure how to sort through it. I would also like to do something like listing data in alphabetical order.
Also, if I created another controller, is it possible for me to list/display the same columns under the new controller?
/views/articles/index.html.erb
<h1>Load Data</h1>
<table>
<tr>
<th>Bullet</th>
<th>Powder</th>
<th>Caliber</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.bullet %></td>
<td><%= article.powder %></td>
<td><%= article.calibertitle %></td>
<td><%= link_to '[View]', article_path(article) %></td>
<td><%= link_to '[Edit]', edit_article_path(article) %></td>
<td><%= link_to '[Delete]', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= link_to 'Back', controller: 'welcome' %>
<p>
</table>
articles_controller.rb
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
/models/article.rb
class Article < ApplicationRecord
validates :calibertitle, presence: true,
length: { minimum: 3 }
end
In your controller assuming you have a column named calibertitle:
def index
#articles = Article.all.order(:calibertitle)
end
To get a Articles with a specific value in a field: Article.where(calibertitle: '308')
You can try this in rails console just to verify. You can also do .reverse_order(:field) if you need it sorted in... reverse.
I'm looking to use the restrict_with_error validation in Rails 4.2.
In this application, an area has multiple apartments. I want to prevent area from being deleted if it has apartments associated with it.
Here is the area model:
class Area < ActiveRecord::Base
has_many :apartments, :dependent => :restrict_with_error
validates :name, presence: true
end
Here is the view where the user can delete an area:
<% #areas.each do |a| %>
<tr>
<td><%= a.id %></td>
<td><%= a.name %></td>
<td><%= a.notes %></td>
<td><%= link_to 'Destroy', a, method: :delete, data: { confirm: 'Are you sure you want to delete this area?' } %></td>
</tr>
<% end %>
</tbody>
</table>
If I try to delete an area that has associated apartments, it is restricted. However, no error is displayed.
This might be a very simple question, but where is the error displayed if the deletion is restricted?
Thanks so much in advance!
You need to access the area errors. You can do so in the area controller like this:
def destroy
unless #area.destroy
flash[:notice] = #area.errors.full_messages[0]
end
redirect_to areas_path
end
I have been butting my head against a wall trying to figure this sucker out.
Basically, I have a 'Quote' model that has 3 fields - content, author and votecount.
Votecount is an integer, and I want to be able to add a vote (increment) from the quotes/index view using a link. This is what I've come up with so far:
views/quotes/index.html.erb
<% #quotes.each do |quote| %>
<tr>
<td><%= quote.content %></td>
<td><%= quote.author %></td>
<td><%= quote.votecount %></td>
<td><%= link_to 'Show', quote %></td>
<td><%= link_to 'Edit', edit_quote_path(quote) %></td>
<td><%= link_to 'Upvote', quote_upvote_path(quote) %></td>
<td><%= link_to 'Destroy', quote, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
quotes_controller.rb
def upvote
#quote = Quote.find(params[:id])
#quote.increment!(:votecount)
redirect_to quotes_path
end
Routes.rb
resources :quotes do
get 'upvote'
end
And this is the error message I receive:
ActiveRecord::RecordNotFound in QuotesController#upvote
Couldn't find Quote with 'id'=
So the action isn't able to find the quote ID, however it's in the actual URL so I'm not sure what I'm bollocksing up here!
Define the upvote action as a member action:
resources :quotes do
get 'upvote', on: :member
end
See the Rails routing documentation for more information.
I'm new to Ruby on Rails & to web programming.
In my application I have two models; Directorate which has_many :users, and User which belongs_to :directorate.
When creating a new user, I use <%= f.collection_select(:directorate_id,Directorate.all, :id, :name) %> in the new.html.erb form to assign the new user to specific directorate. However, I want to build a user-friendly interface for the dba that lists all directorates; and listing all users beside each directorate, with a link to assign any user to a specific directorate.
What I did is the following:
In Directorate model, I defined the following function:
def assign_user!(user)
user.update_attributes(directorate_id: #directorate)
end
and in the directorates controller, I defined the following action:
def assign_user
#directorate = params[:directorate]
assign_user! params[:user]
redirect_to directorates_url
end
Now, directorates/index.html.erb contains the following:
<h1>Listing directorates</h1>
<table>
<tr>
<th>Name</th>
<th>Info</th>
</tr>
<% #directorates.each do |directorate| %>
<tr>
<td><%= directorate.name %></td>
<td><%= directorate.info %></td>
<td><%= link_to 'Show', directorate %></td>
<td><%= link_to 'Edit', edit_directorate_path(directorate) %></td>
<td><%= link_to 'Destroy', directorate, confirm: 'Are you sure?', method: :delete %></td>
<%= #directorate = directorate%>
<%= render 'users_form' %>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Directorate', new_directorate_path %>
and, -users_form.html.erb contains the following form (which is supposed to list all users beside each directorate, with a link to assign any user to a certain directorate):
<h1>Listing Users</h1>
<table>
<tr>
<th>User Name</th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.username %></td>
<td><%= link_to 'Assign to Current Directorate', {controller: 'directorates', action: 'assign_user', directorate: #directorate, user: user}, :method => :put %></td>
</tr>
<% end %>
</table>
<br />
Here is the problem, when listing directorates & click on the 'Assign to Current Directorate' I receive the following error:
http://127.0.0.1:3000/directorates/assign_user?directorate=4&user=5
ActiveRecord::RecordNotFound in DirectoratesController#update
Couldn't find Directorate with id=assign_user
Rails.root: /home/ehab/sites/IAMS
Application Trace | Framework Trace | Full Trace
app/controllers/directorates_controller.rb:61:in `update'
Request
Parameters:
{"_method"=>"put",
"authenticity_token"=>"L5tz3hv2IW0meE79qUq0/tjfGKwDlpC23hOeAWtmTvk=",
"directorate"=>"4",
"user"=>"5",
"id"=>"assign_user"}
It's clear that the params is submitting "id"=>"assign_user" which I don't want, what i want is "id"=>"directorate.id" (4 in the above example). What shall I do to fix this issue?!
first of all your routes should say that assign_user is a member method on a certain directorate object:
resources :directorates do
member do
put :assign_user
end
end
second you say you define assign_user! in Directorate model and assign_user in DirectoratesController but both methods imply that they share same object state like instance variable #directorate which is not true
your controller method assign_user should look vaguely like
def assign_user
#directorate = Directorate.find params[:id]
#user = User.find params[:user_id]
#directorate.assign_user! #user
end
and model method should look like
def assign_user!(user)
user.update_attributes(directorate_id: self.id)
end
and even that i would switch around to instead of telling Directorate to change user's attributes you would tell User to assign itself to whatever controller wants.
and the final bit is your link that assigns user to directorate:
link_to 'Assign to Current Directorate',
assign_user_directorates_path(#directorate, :user_id => user)
0 lines of code above were tested for even syntactical correctness, DO NOT copy-paste, read and understand