When trying csv export in rails, I am getting these values only
`#<User:0x007f4a41859980> #<User:0x007f4a41835c88>
my controller file ps4_controller.rb
def csv_export
#users = User.order(created_at: :desc)
respond_to do |format|
format.html
format.csv { render text: #users.to_csv }
format.xls { render text: #users.to_csv(col_sep: "\t") }
end
end
model file ps4.rb
class Ps4 < ActiveRecord::Base
attr_accessible :username, :email, :school, :batch
def self.to_csv(users)
CSV.generate do |csv|
csv << column_names
users.each do |ps4|
csv << ps4.attributes.values_at(*column_names)
end
end
end
end
view file csv_export.xls.rb
<table>
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>School</th>
<th>Batch</th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= user.school %></td>
<td><%= user.batch %></td>
</tr>
<% end %>
</tbody>
</table>
Can anybody plz help?
In controller:
respond_to do |format|
...
format.csv { send_data #users.to_csv, filename: "users-#{Date.today}.csv" }
end
In model (do not send any attributes to to_csv method):
def self.to_csv
attributes = %w{id email}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |user|
csv << attributes.map{ |attr| user.send(attr) }
end
end
end
#users is an array of users, and you are trying to call to_csv method on the array. On the other hand the to_csv method is defined as a class method, meaning you need to invoke it on the class itself:
format.csv { render text: User.to_csv(#users) }
Related
i have a table with set of students, I want update attendance column to all the students with different values using single form which will save all the record.
note: i was able to save all the records individually.
here is my edit page
<table>
<thead>
<tr>
<th>Name</th>
<th>user_id</th>
<th>Present days</th>
<th>Total days</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #attendancce.each do |userr| %>
<tr>
<%= form_for(userr) do |f| %>
<td><%=userr.user.name %></td>
<td><%= userr.user_id %></td>
<td><%= f.number_field :present_days %></td>
<td><%= f.number_field :total_days %></td>
<td><%= f.submit %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "submit" %>
and below is my method for updating the records
def update
#attendancce = Attendance.all
respond_to do |format|
if #attendance.update(attendance_params)
format.html { redirect_to #attendance, notice: 'Attendance was successfully updated.' }
format.json { render :show, status: :ok, location: #attendance }
else
format.html { render :edit }
format.json { render json: #attendance.errors, status: :unprocessable_entity }
end
end
end
please suggest how to achieve my target
im new with rails. currently im facing problem with it. i have 3 model which is
order, task, and order task.
in the ordertask, i try to pass id from show.html to edit.html, but the rails only pass
http://localhost:3000/orders_tasks/%23%3COrderTask::ActiveRecord_Relation:0x00000009c9a078%3E/edit
instead of id number.
here is my show.html.erb
<p id="notice"><%= notice %></p>
<table class="table table-hover">
<tr>
<td>Task
</td>
<td>Status:
</td>
</tr>
<tr>
<% #status.each do |i| %>
<td><%= i.task_id %>
</td>
<td><%= i.status %>
</td>
</tr>
<% end %>
</table>
<%= link_to 'Edit', edit_orders_task_path(#status) %> |
<%= link_to 'Back', orders_tasks_path %>
here is my ordertask controller
class OrdersTasksController < ApplicationController
before_action :set_status, only: [:show, :edit, :update]
def index
#orders = Order.all
#status = OrderTask.includes(:task,:order).where(order_id: params[:id])
end
def edit
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,:order).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(:order_id,:status)
end
end
and here is my edit.html
<%= form_for(#status, html: {class: 'form form-horizontal'}).first.id 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 %>
my order model
class Order < ActiveRecord::Base
belongs_to :staff
belongs_to :customer
belongs_to :service
has_many :order_task
has_many :tasks , through: :order_task
validates :staff_id , presence: true
end
my task model
class Task < ActiveRecord::Base
has_many :order_task
has_many :orders , through: :order_task
attr_accessor :status, :task_type
validates :task_name, presence: true, uniqueness: true
end
and lastly my order task model
class OrderTask < ActiveRecord::Base
self.primary_key = [:order_id]
self.table_name = "Orders_tasks"
belongs_to :order
belongs_to :task
end
i hope you guys can help me out cause i have stuck with this problem for a week now. :D
You should place the edit links in the loop... since #status contains multiple ordertaks, and each ordertask should have his own edit page:
<p id="notice"><%= notice %></p>
<table class="table table-hover">
<tr>
<td>Task
</td>
<td>Status:
</td>
<td>Action:</td>
</tr>
<tr>
<% #status.each do |i| %>
<td><%= i.task_id %>
</td>
<td><%= i.status %>
</td>
<td><%= link_to 'Edit', edit_orders_task_path(i) %></td>
</tr>
<% end %>
</table>
<%= link_to 'Back', orders_tasks_path %>
Edit: But I do like to point out that most of the time you're not in need of Controllers for your join models. So I do not know why you think you need it, most of the logic can be build from either the OrdersController or the TasksController.
First post here... doing the ROR tutorial and found similar questions regarding this issue... but of course none worked. Anyway following the tutorial everything works except when clicking on this link I get the error The action 'edit1' could not be found for SubjectsController... any help is greatly appreciated!Myself and the tutorial are both using same version too... 4.0.
SubjectsController:
class SubjectsController < ApplicationController
layout false
def index
#subjects = Subject.sorted
end
def show
#subject = Subject.find(params[:id])
end
def new
#subject = Subject.new({:name => "Default"})
end
def create
# Instantiate a new object using form parameters
#subject = Subject.new(subject_params)
# Save the object
if #subject.save
# If save succeeds, redirect to the index action
redirect_to(:action => 'index')
else
# If save fails, redisplay the form so user can fix problems
render('new')
end
end
def edit
#subject = Subject.find(params[:id])
end
def update
# Find an existing object using form parameters
#subject = Subject.find(params[:id])
# Update the object
if #subject.update_attributes(subject_params)
# If update succeeds, redirect to the index action
redirect_to(:action => 'show', :id => #subject.id)
else
# If update fails, redisplay the form so user can fix problems
render('edit')
end
end
def delete
#subject = Subject.find(params[:id])
end
def destroy
subject = Subject.find(params[:id]).destroy
redirect_to(:action => 'index')
end
private
def subject_params
# same as using "params[:subject]", except that it:
# - raises an error if :subject is not present
# - allows listed attributes to be mass-assigned
params.require(:subject).permit(:name, :position, :visible)
end
end
Edit.html
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects edit">
<h2>Update Subject</h2>
<%= form_for(:subject, :url => {:action => 'update', :id => #subject.id}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visible</th>
<td><%= f.text_field(:visible) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Update Subject") %>
</div>
<% end %>
</div>
routes file
SimpleCms::Application.routes.draw do
root "demo#index"
#get "demo/index"
match ':controller(/:action(:id))', :via => [:get, :post]
Other than this I'm on Chrome and windows 7.
Code for Show link:
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects show">
<h2>Show Subject</h2>
<table summary="Subject detail view">
<tr>
<th>Name</th>
<td><%= #subject.name %></td>
</tr>
<tr>
<th>Position</th>
<td><%= #subject.position %></td>
</tr>
<tr>
<th>Visible?</th>
<td><%= #subject.visible ? 'true' : 'false' %></td>
</tr>
<tr>
<th>Created</th>
<td><%= #subject.created_at %></td>
</tr>
<tr>
<th>Updated</th>
<td><%= #subject.updated_at %></td>
</tr>
</table>
</div>
Code for delete link:
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects delete">
<h2>Delete Subject</h2>
<%= form_for(:subject, :url => {:action => 'destroy', :id => #subject.id}) do |f| %>
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name"><%= #subject.name %></p>
<div class="form-buttons">
<%= submit_tag("Delete Subject") %>
</div>
<% end %>
</div>
Your controller contains edit action, but not edit1. edit1 is a result of action being concatenated with parameter due to invalid route setup.
In your routes config a slash / is missing
It should be like this
match ':controller(/:action(/:id))', :via => [:get, :post]
^
|
Adding a slash to the 'match' route as Mladen suggests will fix your immediate problem. However, having that default route is no longer recommended; it's better to be explicit about your routes.
This will give you the 'standard' REST-ful routes, to replace the 'match' route in your routes file:
resources :subjects
If you're really using a 'GET delete' action in your controller you would need a 'member' entry to add that, but usually there's just a link on the show and/or edit form that goes right to the 'destroy' action to delete a record; not sure why you would need a separate 'delete' view.
I have added gem 'acts-as-taggable-on' -v 2.3.1 in Gemfile.
Rails version 3.2.13, Ruby -v 1.9.3
In article.rb,
class Article < ActiveRecord::Base
acts_as_taggable_on :tags
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5 }
end
In articles_controller.rb,
class ArticlesController < ApplicationController
before_filter :authenticate_author!, except: [:index, :show]
def index
myarray = Article.all
#articles = Kaminari.paginate_array(myarray).page(params[:page]).per(10)
end
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def edit
#article = Article.find(params[:id])
end
def update
#article = Article.find(params[:id])
if #article.update_attributes(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :tag_list => [])
end
end
In articles/_form.html.erb
<div class="field">
<%= f.label :tag_list, "Tags (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
I ran the following commands after installing the acts-on-taggable-gem,
rails generate acts_as_taggable_on:migration
rake db:migrate
In articles/index.html.erb
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article.tag_list %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<% if author_signed_in? %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end%>
</tr>
<% end %>
The tags are not being displayed in the index page.
But if I do ,
article = Article.new(:title => "Awesome")
article.tag_list = "awesome, cool"
article.save
The transaction gets committed and the tags are displayed on the browser.
Why aren't the tags getting saved and displayed in the index page?
The Tag List is an array, which means you need to 'loop' to extract the data. Whenever I have used acts_as_taggable I run through a loop to extract the tags. Something like the following should work:
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td>
<% article.tag_list.each do |tag| %>
<%= tag %>
<% end %>
</td>
<td><%= link_to 'Show', article_path(article) %></td>
<% if author_signed_in? %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end%>
</tr>
<% end %>
I want to update a model on my db. if an user checked the checkbox, it must not be appear again on the view.
I have some view like this:
<%= form_tag( riepilogo_path, method: "post", id: "sel") do %>
<%= hidden_field_tag "sala", params[:sala] %>
<%= hidden_field_tag "spectacle_id", params[:spectacle_id] %>
<%= hidden_field_tag "num", params[:num] %>
<table>
<tr>
<th></th>
<th>Numero</th>
</tr>
<% for posti in #postis %>
<tr>
<td><%= check_box_tag "posti_ids[]", posti.id %></td>
<td><%=h posti.numero %></td>
</tr>
<% end %>
</table>
<%= submit_tag "OK", id: "sub"%>
<% end %>
controller of Postis is something like so:
class PostisController < ApplicationController
def index
#postis = Posti.where(:stato => "unchecked" , :spectacle_id => params[:spectacle_id] , :hall_num => params[:sala])
end
def posti_multiple
#postis = Posti.find(params[:posti_ids])
end
end
In the Posti's model i have attributes: spectacle_id, hall_num, seat(integer) and stato (:default => "unchecked").
When he submit, seats be load in posti_ids[]. I want to update stato of seats which are present in posti_ids from "unchecked" to "checked".
As per your comment,you have tried to perform update_column on an Array.So is the error
undefined method `update_column' for # Array:0xb881584
Here #postis returns.You should be giving it like this
Posti.update_column(stato: "checked")