What I am trying to accomplish is a simple "toggle" checkbox for On/Off for a view which contains records from a model.
I have attempted to look into a "hidden" value, but it doesn't appear to work as intended.
How to get blank checkboxes to pass as false to params
When I've added: <%= hidden_field_tag "category_ids[]", '' %>
I receive Couldn't find Category with 'category_name'= when unchecking.
Essentially, the table is a Model.all, but I want to be able to modify the key value on an is_active column to true or false, depending on if the box is checked, or not. Currently, I can accomplish this for "checking" the box. But, not unchecking (passes null). I am trying accomplish this is one swoop rather than making all my checkes, and another pass for my unchecked. And, also bypassing the show/edit process. The table size is rather small, so I am not concerned with latency.
I have attempted to search as much as I could, but am at a loss. With what I've found I can do one or the other, but not both unfortunately, and I would greatly appreciate any guidance.
My view:
<h4>Categories</h4>
<%= form_tag enable_categories_path, method: :put do |f| %>
<table id="myTable" class="table table-bordered table-striped">
<tr>
<th>Enable</th>
<th>Category Name</th>
<th>Active</th>
</tr>
<tbody>
<% #categories.each do |category| %>
<tr>
<td>
<%= check_box_tag "category_ids[]", category.id, category.is_active == 1 ? 'checked' : nil %>
</td>
<td><%= link_to category.category_name, category %></td>
<td><%= category.is_active == 1 ? 'Yes' : 'No' %></td>
</tr>
<% end %>
</tbody>
</table>
<%= render 'settings/button' %>
<% end %>
Here, the checkboxes are grabbing their state from the model itself for the corresponding record, so if no action is taken on the checkbox it remains the same (or passes state back)
My controller:
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update]
def index
#categories = Category.all.order(:category_sort)
end
def show
#category = Category.find(params[:id])
end
def edit
end
def update
if #category.update(category_params)
redirect_to categories_path
else
render :edit
end
end
def enable
Category.update(params[:category_ids], params[:category_ids].map{ |e| {is_active: true} })
redirect_to categories_path
end
private
def set_category
#category = Category.find(params[:id])
end
def category_params
params[:category].permit(:id, :category_name, :is_active)
end
end
Currently, I'm only passing is_active: true, until I can figure a way to pass ALL checkbox states.
My model:
class Category < ActiveRecord::Base
self.table_name = 'categories'
has_many :metrics
end
My route:
resources :categories do
collection do
put :toggle
end
end
Everything appears to pass correctly for checked boxes. But, nothing appears in the logs for when something is unchecked.
When I've run into situations like this in Rails I usually end up using AJAX rather than mass assignment. It's actually rather easy. At least easier for me than learning the inner workings of the check_boxes and collection_check_boxes, LOL
A simple Categories table:
<table>
<thead>
<tr>
<th>Category</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<% #categories.each do |category| %>
<tr>
<td><%= category.category_name %></td>
<% status = category.is_active? ? 'Active' : 'Inactive' %>
<td id="<%= category.id %>" ><button class="button_<%= status %>"><%= link_to status, toggle_active_path(:id => category.id), :method => :post, :remote => true %></button></td>
</tr>
<% end %>
</tbody>
</table>
The lines to note are the embedded ruby setting the status variable. This is used to set the value of the class for the button. In the CSS file the button class button_Active sets the color to green and button_Inactive makes it red. It also uses this variable to set the text of the button.
Create a method in the controller that toggles the status:
def toggle_is_active
#category.toggle!(:is_active) #flips the boolean on is_active
respond_to do |format|
format.html { redirect_to(#category) }
format.js
end
Be aware that .toggle! will bypass model validations and save. If you need to ensure validations run you can use #category.toggle(:is_active).save
This will respond to format.js and call a very small file called toggle_is_active.js.erb:
$('#<%= #category.id %>').html('<%= escape_javascript(render :partial => 'toggle_active') %>');
This targets the id of the html element that we set to the id of the category in the table row.
This calls a partial called _toggle_active.html.erb and updates the appropriate <td> with the new html:
<% status = #category.is_active? ? 'Active' : 'Inactive' %>
<button class="button_<%= status %>"><%= link_to status, toggle_active_path(:id => #category.id), :method => :post, :remote => true %></button>
Now all you need is a route to access the AJAX link:
routes.rb:
post 'toggle_active' => 'categories#toggle_is_active'
Here is a working example you can clone on github. It has the stylesheets to get the look above. I think you can extrapolate this for almost any situation:
https://github.com/Beartech/category_boxes
Let's consider what's going on here:
def enable
Category.update(params[:category_ids], params[:category_ids].map{ |e| {is_active: true} })
redirect_to categories_path
end
Can you post what params[:category_ids] looks like here? the map here doesn't quite make sense. Also, what is the data type of is_active in the database?
Related
Ok this is going to be a bit long-winded so bear with me.
I have my album index page displaying all my albums, I can re order them based on the time created and the likes. my code for doing this is as follows (just ignore that some stuff is commented out, i havent yet figured out how to do weekly top like and all-time top likes, but thats another issue for another post)
# album.rb
scope :latest, -> { order(created_at: :desc) }
#scope :weekly, -> { order(likes: :desc) }
#scope :alltime, -> { order(likes: :desc) }
def self.sort_by(sort_param)
case sort_param
when 'latest'
latest
#when 'weekly'
#weekly
#when 'alltime'
#alltime
else
latest
end
end
#album controller
def index
#albums = Album.sort_by(params[:order])
end
#album index
<table>
<tbody>
<%= link_to "Latest", albums_path(order: :latest) %><br>
<%= link_to "Weekly Top", albums_path(order: :weekly) %><br>
<%= link_to "All-Time Top", albums_path(order: :alltime) %>
</tbody
</table>
<table>
<tbody>
<% #albums.each do |album| %>
<tr>
<td><%= link_to album.name, album %></td>
<td><%= link_to image_tag(album.cover_image.variant(resize: "200x200")), album %></td>
<td><%#= album.videos #%></td>
</tr>
<% end %>
</tbody>
</table>
ok so like I aid that has all worked as expected and changes the order of my albums in the album index page based off whichever link is selected... now onto my actual issue, I have all albums tied to a category. When I go into my categories and select one it takes me to the category show page for that category and displays all albums that fall within that specific category, great!. Now what im struggling with is how can I reorder the albums within the category show page like I did in the album index page above?
I have tried a few different combinations of things but unable to make it work. Here is what I have so far
#category.rb
scope :latest, -> { order(created_at: :desc) }
#scope :weekly, -> { order(likes: :desc) }
#scope :alltime, -> { order(likes: :desc) }
def self.sort_by(sort_param)
case sort_param
when 'latest'
latest
#when 'weekly'
#weekly
#when 'alltime'
#alltime
else
latest
end
end
#category controller
def show
#categories = Category.sort_by(params[:order])
end
#category show
<table>
<tbody>
<%= link_to "Latest", categories_path(order: :latest) %><br>
<%= link_to "Weekly Top", categories_path(order: :weekly) %><br>
<%= link_to "All-Time Top", categories_path(order: :alltime) %>
</tbody
</table>
<table>
<tbody>
<% #category.albums.map.each do |album| %>
<tr>
<td><%= link_to album.name, album %></td>
<td><%= link_to image_tag(album.cover_image.variant(resize: "200x200")), album %></td>
</tr>
<% end %>
</tbody>
</table>
So Im pretty sure I have it wrong in the category controller part, as well as the paths im calling in the show page.
Also im not even sure if I had to put the code in the Category model or if I would be able to call the code from the Album model as I am trying to re-order the albums in the category show page.
Any advice on this would be greatly appreciated. Thanks in advance.
Assuming that likes is an integer column:
In order to get albums and categories created this week you'll need to define scopes like this:
scope :weekly, -> { where("created_at <= ?", DateTime.current.beginning_of_the_week) }
scope :latest, -> { order(created_at: :desc) }
scope :by_likes, -> { order(likes: :desc) }
Notice that "weekly" is not really an order but a filter. When you won't apply it you'll get "all the time".
Now in the views you can do:
<%= link_to "Latest", categories_path(order: :latest) %><br>
<%= link_to "Weekly Top", categories_path(weekly: true, order: :by_likes) %><br>
<%= link_to "All-Time Top", categories_path(order: :by_likes) %>
and the analogical thing for the albums.
Now in the controllers you can do:
# categories controller
def show
#categories = Category.find(params[:id])
#albums = #category.albums
#albums = #albums.weekly if params[:weekly]
#albums = #albums.by_likes if params[:order] == "by_likes"
#albums = #albums.latest if params[:order] == "latest"
end
# albums controller
def index
#albums = Album.all
#albums = #albums.weekly if params[:weekly]
#albums = #albums.by_likes if params[:order] == "by_likes"
#albums = #albums.latest if params[:order] == "latest"
end
And then you can just use the #albums variable in the category show view like this:
#albums.each do |album|
I am creating a list of students and when I click on one of the hyperlinks, I want to display a table with the student's information I clicked on.
I am working with two pages, index.html.erb and show.html.erb. In the index page, I want to display the list and the show page, I want to display the information for only one student. I am very new to Ruby on Rails, but I want to get better at it. Thank for all your responses.
index.html.erb
<h1> Welcome </h1>
<%= link_to "Display Table", students_show_path %>
<%= link_to "Form", students_new_path %>
<ol> Students
<% #student.each do |s| %>
<!--<li><%= link_to s.FirstName, students_show_path(#student.students_id), method: :post %> </li> -->
<li><%= link_to s.FirstName, :action => "show", :id => Student.id %> </li>
<% end %>
</ol>
This is my show.html.erb
Table's Page
<% #student.each do |s| %>
<tr>
<td> <%= s.FirstName %> </td>
<td> <%= s.LastName %> </td>
<td> <%= s.NickName %> </td>
<td> <%= s.EmailAddress %> </td>
<td> <%= s.Birthday %> </td>
<td> <%= s.MedicalNotes %> </td>
<td> <%= s.Grade %> </td>
<td> <%= s.School %> </td>
<td> <%= s.Gender %> </td>
</tr>
This is my routes.rb
Rails.application.routes.draw do
root 'students#index'
get 'students/index'
get 'students/show'
get 'students/new'
get 'students/update'
get 'students/create'
get 'students/edit'
get 'students/destroy'
get '/signup', to: 'students#new'
post '/signup', to: 'students#create'
post '/index', to: 'students#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :students
# generates:
# get "/students" -- index on your controller
# get "/students/:id" -- show on your controller
# get "/students/new" -- new method on your controller
# post "/students" -- create on your controller
# get "/students/:id/edit" -- edit method on your controller
# put "/students/:id" -- update on your controller
# patch "/students/:id" -- update on your controller
# delete "/students/:id" -- destroy on your controller
end
This is my students_controller
class StudentsController < ApplicationController
def index
#student = Student.all
end
def show
id = params[:id]
#student = Student.where("Student id = ?" , "#{id}")
end
def new
#student = Student.new
end
def update
end
def create
#student = Student.new(student_params)
if #student.save
redirect_to students_show_path
else
render 'new'
end
end
def edit
end
def destroy
end
private
def student_params
params.permit(:FirstName, :LastName, :NickName, :EmailAddress, :Birthday, :MedicalNotes, :Grade, :School, :Gender)
end
end
For your index.html.erb file, try updating the link_to helpers to something like:
<%= link_to s, s.FirstName %>
You don't need to pass the full route + ID if using link helpers with the show method, rails will automagically link for you.
Remove all of the get 'students/... routes, just leave the resources: :students. The routes you're specifying above this line are redundant.
Your show method in the controller can also be cleaned up slightly:
def show
#student = Student.find(params[:id])
end
And finally, I believe in your create method, you can redirect using just the resource:
def create
#student = Student.new(student_params)
if #student.save
redirect_to #student
else
render 'new'
end
First thing:
The Ruby way to name attributes, methods, and variables is to use snake_case. For example, s.FirstName should be s.first_name. In Ruby, if the name is capitalized like FirstName it is actually a constant. This style is called CamelCase and it is the conventional way to name classes in Ruby e.g. StudentsController.
For the routes.rb:
Rails will generate all of the RESTful routes and helper methods for your resource if you use
resources :students
you can then view all of these routes by running rake routes on the command line or visiting http://localhost:3000/rails/info/routes in the browser. You can learn more info about routes in the documentation: http://guides.rubyonrails.org/routing.html#inspecting-and-testing-routes
You can then use the Rails link_to helper method like this:
<%= link_to s.first_name, s %>
and Rails will figure out the id of the object for you. Or if you want to be more explicit:
<%= link_to s.first_name, student_path(s.id) %>
And in the show action of your StudentsController you want:
def show
#student = Student.find(params[:id])
end
this will look up the student record in the database based on the :id parameter in the URL for student show page
Two things I notice about your code
1)
get 'students/show'
To be able to retrieve an user, you need a user id, a unique identifier. So your show route should contain an id. The Rails way is something like this
get 'students/:id'
I'm simplyfing here because your route could work if you call it with your param in query string ('students/show?id=something')
Anyway, you are defining every route in a different line. It turns out that you can define all of them with just the line
resources :students
2)
In your show action you want just one user. And you have an id, and that's unique, so you can use it to directly retrieve your user
def show
#student = Student.find(params[:id])
end
You used where and that's for retrieve ALL the user that match your condition (maybe you want to retrieve every user whose first name is 'John'). So it turns out your #user doesn't contain directly one user, but your user in a relation. That's because where.
currently im accessing my join table directly. the reason im doing this is because i only want the staff to be able to adjust the status
im facing problem with
undefined method order_task_path' for #<#<Class:0x00000009bf62c0>:0x0000000a6d7c70>`
with parameter Parameters:
{"id"=>"1,1"}
i'm able to display the join table according to my need. the problem is when i try to display it in edit.
here is my ordertask controller
class OrdersTasksController < ApplicationController
before_action :set_status, only: [:show]
def index
#orders = Order.all
##status = OrderTask.includes(:task,:order).where(order_id: params[:id])
end
def edit
#status = OrderTask.find(params[:id])
end
def show
end
def update
respond_to do |format|
if #status.update(order_params)
format.html { redirect_to #status, notice: 'Order was successfully updated.' }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit }
format.json { render json: #status.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_status
#status = OrderTask.includes(:task).where(order_id: params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order_task,:order).permit(:id,:order_id,:status)
end
end
my show.html.erb
<p id="notice"><%= notice %></p>
<table class="table table-hover">
<tr>
<td><h4>Order Number : <%= #status.first.order.order_number %></h4>
</td>
</tr>
<tr>
<td>Task
</td>
<td>Status:
</td>
</tr>
<tr>
<% #status.each do |i| %>
<td><%= i.task.task_name %>
</td>
<td><%= i.status %>
</td>
<td><%= link_to 'Edit', edit_orders_task_path(i) %></td>
</tr>
<% end %>
</table>
<%= link_to 'Back', orders_tasks_path %>
my _form.html.erb
<%= form_for(#status) do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<table class="table table-hover">
<tr>
<td><%= f.label "List of task" %>
</td>
<td><%= f.label "Status" %>
</td>
</tr>
<tr>
<td><%= f.task_id %>
</td>
<td><div class="dropdown">
<%= f.select(:status,['In progress', 'Completed'], {}, {class: "control"})%> </div>
</td>
</tr>
<tr>
<td>
</td>
<td><%= f.submit %>
</td>
</tr>
</table>
<% end %>
and my routes
resources :orders
resources :services
resources :tasks
resources :customers
resources :staffs
resources :orders_tasks
root 'staffs#index'
and lastly my ordertask model
class OrderTask < ActiveRecord::Base
self.primary_key = [:order_id,:task_id]
self.table_name = "Orders_tasks"
belongs_to :order
belongs_to :task
end
hope u guys can give suggestion or something to help me with this. thx in advance.
EDIT
here is my route.rb
C:\Users\Idea\DHMS>rake routes
Prefix Verb URI Pattern Controller#Action
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
new_order GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PATCH /orders/:id(.:format) orders#update
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
services GET /services(.:format) services#index
POST /services(.:format) services#create
new_service GET /services/new(.:format) services#new
edit_service GET /services/:id/edit(.:format) services#edit
service GET /services/:id(.:format) services#show
PATCH /services/:id(.:format) services#update
PUT /services/:id(.:format) services#update
DELETE /services/:id(.:format) services#destroy
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PATCH /tasks/:id(.:format) tasks#update
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
customers GET /customers(.:format) customers#index
POST /customers(.:format) customers#create
new_customer GET /customers/new(.:format) customers#new
edit_customer GET /customers/:id/edit(.:format) customers#edit
customer GET /customers/:id(.:format) customers#show
PATCH /customers/:id(.:format) customers#update
PUT /customers/:id(.:format) customers#update
DELETE /customers/:id(.:format) customers#destroy
staffs GET /staffs(.:format) staffs#index
POST /staffs(.:format) staffs#create
new_staff GET /staffs/new(.:format) staffs#new
edit_staff GET /staffs/:id/edit(.:format) staffs#edit
staff GET /staffs/:id(.:format) staffs#show
PATCH /staffs/:id(.:format) staffs#update
PUT /staffs/:id(.:format) staffs#update
DELETE /staffs/:id(.:format) staffs#destroy
orders_tasks GET /orders_tasks(.:format) orders_tasks#index
POST /orders_tasks(.:format) orders_tasks#create
new_orders_task GET /orders_tasks/new(.:format) orders_tasks#new
edit_orders_task GET /orders_tasks/:id/edit(.:format) orders_tasks#edit
orders_task GET /orders_tasks/:id(.:format) orders_tasks#show
PATCH /orders_tasks/:id(.:format) orders_tasks#update
PUT /orders_tasks/:id(.:format) orders_tasks#update
DELETE /orders_tasks/:id(.:format) orders_tasks#destroy
root GET / staffs#index
According to the error message undefined method order_task_path' for #<#<Class:0x00000009bf62c0>:0x0000000a6d7c70>, the url helper method does not exist.
you can rake rake routes in the terminal to get all the routes and route helpers.
resources :orders do
resources :tasks
end
generate the following routes and helpers.
order_tasks GET /orders/:order_id/tasks(.:format) tasks#index
POST /orders/:order_id/tasks(.:format) tasks#create
new_order_task GET /orders/:order_id/tasks/new(.:format) tasks#new
edit_order_task GET /orders/:order_id/tasks/:id/edit(.:format) tasks#edit
order_task GET /orders/:order_id/tasks/:id(.:format) tasks#show
PATCH /orders/:order_id/tasks/:id(.:format) tasks#update
PUT /orders/:order_id/tasks/:id(.:format) tasks#update
DELETE /orders/:order_id/tasks/:id(.:format) tasks#destroy
resources :orders do
resources :orders_tasks
end
generate the following routes and helpers.
order_orders_tasks GET /orders/:order_id/orders_tasks(.:format) orders_tasks#index
POST /orders/:order_id/orders_tasks(.:format) orders_tasks#create
new_order_orders_task GET /orders/:order_id/orders_tasks/new(.:format) orders_tasks#new
edit_order_orders_task GET /orders/:order_id/orders_tasks/:id/edit(.:format) orders_tasks#edit
order_orders_task GET /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#show
PATCH /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#update
PUT /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#update
DELETE /orders/:order_id/orders_tasks/:id(.:format) orders_tasks#destroy
http://guides.rubyonrails.org/routing.html#nested-resources
This app consists of a form to submit and currently I am trying to print a few rows of a table. This is working, but unfortunately I am also getting a single long string of the entire database table attributes. There is nothing in the code that I have written (I believe) which would cause this. I fear that this is some unseen rails magic, any insight would be great!
The controller:
class StudentsController < ApplicationController
def new
#student = Student.new
end
def create
student_map = {"student_id" => params[:student_id], "student_name" => params[:student_name],
"major" => params[:major], "minor" => params[:minor], "other_information" => params[:other_information],
"class_year_id" => params[:class_year_id], "hours_st" => params[:hours], "qr_id" => qr_id,}
if (newStudentRow.save)
redirect_to action: 'index'
else
render action: 'new'
end
end
def index
#students = Student.all
end
end
The Index view:
<h1>Students#index</h1>
<p>Find me in app/views/students/index.html.erb</p>
<table>
<thead>
<tr>
<th>Student Name</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<%= #students.each do |s| %>
<tr>
<td><%= s.student_name %></td>
<td><%= s.student_id %></td>
</tr>
</tbody>
<% end %>
</table>
After entering data and submitting the form, this link shows the following output:
Thanks for the help!
Change:
<%= #students.each do |s| %>
To this:
<% #students.each do |s| %>
In Ruby, each executes the block for each element AND returns the array. Having the = outputs the array, which is why you are seeing that long string.
I'm fairly new to rails so please bear with me.
I have a resource :examen with a has_one relation to a resource :aanwezigheidslijst.
In the aanwezigheids_controller i have everything prepared for editing "aanwezigheidslijst".
Now the problem I'm having is when editing (specificly showing whether or not a student was attending or not by checking a checkbox) each "aanwezigheidslijst" is when i submit the form it renders to 'index' (just like i want) but apparently no changes have been made to the mysql database.
There are no errors that pop up and the lay-out is like i want it, it's just the checkbox that represents the boolean "aanwezig" that isn't updated when i submit it.
The connection with the mysql database works fine with other parts of the app.
any solutions are very much appreciated.
EDIT
as proposed i took a look at the log file. I figure the Unpermitted parameters have something to do with it? can anyone clarify?
Log File:
Started PATCH "/aanwezigheidslijst/2/edit" for 127.0.0.1 at 2015-02-21 16:12:14 +0100
Processing by AanwezigheidslijstController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"uxgvXaOPs66wtYBntTr907RN4yfC0vLTfresXPURBow=", "aanwezigheidslijst"=>{"1"=>{"student"=>"Tom Nys", "aanwezig"=>"1"}, "2"=>{"student"=>"Jan de Roeck", "aanwezig"=>"1"}, "3"=>{"student"=>"Nick van Heertum", "aanwezig"=>"0"}}, "commit"=>"Save Aanwezigheidslijst", "id"=>"2"}
[1m[35mAanwezigheidslijst Load (0.0ms)[0m SELECT `aanwezigheidslijsts`.* FROM `aanwezigheidslijsts` WHERE `aanwezigheidslijsts`.`id` = 2 LIMIT 1
Unpermitted parameters: 1, 2, 3
[1m[36m (1.0ms)[0m [1mBEGIN[0m
[1m[35m (0.0ms)[0m COMMIT
Redirected to http://localhost:3000/aanwezigheidslijst/index
Completed 302 Found in 8ms (ActiveRecord: 1.0ms)
aanwezigheidslijst migration:
class CreateAanwezigheidslijsts < ActiveRecord::Migration
def change
create_table :aanwezigheidslijsts do |t|
t.string :student
t.boolean :aanwezig
t.timestamps
end
end
end
aanwezigheids_controller.rb:
class AanwezigheidslijstController < ApplicationController
def index
#aanwezigheidslijst = Aanwezigheidslijst.all
#examen = Examen.all
end
def edit
#aanwezigheidslijst = Aanwezigheidslijst.where(:examen_id => params[:id]).all
end
def update
#aanwezigheidslijst = Aanwezigheidslijst.find(params[:id])
if #aanwezigheidslijst.update(aanwezig_params)
redirect_to aanwezigheidslijst_index_path
else
render 'edit'
end
end
private
def aanwezig_params
params.require(:aanwezigheidslijst).permit(:student, :aanwezig)
end
end
edit.html.erb:
<div class="tblAanwezig">
<div class="col-xs-6 col-sm-3">
<table class="table table-bordered table-condensed" style="max-width: 300px;">
<thead>
<tr>
<th scope="col">Student</th>
<th scope="col">Aanwezig</th>
</tr>
</thead>
<%= form_tag edit_aanwezigheidslijst_url, :id => 'al_form', method: :patch do %>
<% #aanwezigheidslijst.each do |aanwezigheidslijst| %>
<%= fields_for "aanwezigheidslijst[]", aanwezigheidslijst do |al| %>
<tbody>
<tr>
<td> <%= al.text_field :student %></td>
<td><%= al.check_box :aanwezig %></td>
</tr>
</tbody>
<% end %>
<% end %>
</table>
<%= submit_tag "Save Aanwezigheidslijst" %>
<% end %>
</div>
</div>
routes.rb
Rails.application.routes.draw do
get 'aanwezigheidslijst/index'
patch 'aanwezigheidslijst/:id/edit' => 'aanwezigheidslijst#update'
get 'examen/index'
get 'comments/new'
post 'comments/new'
get 'comments/update'
get 'comments/create'
get 'application/application'
resources :comments
resources :aanwezigheidslijst
root 'comments#index'
end