Can someone rails-haml expert translate please , how this haml code is translated to html ?
the page is derived from this url . I m only interested in lines 1 and 12. I ve used the various tools , on line con verters but the dont seem to work :(
%table.table.table-bordered.table-striped#sortable{:data => {update_url:
sort_admin_things_path}}
%thead
%tr
%th= Title
%th= Description
%th
%tbody
- #things.each do |thing|
# make the <tr>'s draggable by adding the expected '.item' class
# pass the item id into params[:id]
%tr{data: {item_id: "#{thing.id}"}, class: 'item'}
%td= thing.title
%td= thing.description.truncate(20)
%td
= link_to 'show', admin_thing_path(thing), :class => 'btn'
= link_to 'edit', edit_admin_thing_path(thing), :class => 'btn btn-primary'
= link_to 'destroy', admin_thing_path(thing), method: :delete, confirm: "Are you sure?", :class => 'btn btn-danger'
That's not correctly indented. The big deal with haml is that things must be correctly indented to work. Otherwise you'll have unexpected errors due to the indentation.
Anyway..Try this:
<table class="table table-bordered table-striped" id="sortable" data-update_url = <%= sort_admin_things_path%> >
<thead>
<tr>
<th>
<%= Title %>
</th>
<th>
<%= Description %>
</th>
<th> </th>
</tr>
</thead>
<tbody>
<% #things.each do |thing| %>
<tr data-item_id= <%= thing.id %> class= "item" >
<td>
<%= thing.title %>
</td>
<td>
<%= thing.description.truncate(20) %>
</td>
<td>
<%= link_to 'show', admin_thing_path(thing), :class => 'btn' %>
<%= link_to 'edit', edit_admin_thing_path(thing), :class => 'btn btn-primary' %>
<%= link_to 'destroy', admin_thing_path(thing), method: :delete, confirm: "Are you sure?", :class => 'btn btn-danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>
Related
I am new to rails and wants to do following tasks with my uploader-
1) view the uploaded excel sheets in rails in editable mode
2)save that edited sheet in database(sqlite3).
I am seriously not getting what to do with this "view method"(in controller) to get my solution that's why leaving view method empty.
Also, please explain what is "resume.attachment_url" doing in
<%= link_to "Download Resume", resume.attachment_url %> in index.html.erb .
Thanks,
Aarzoo Goyal
routes.rb
Rails.application.routes.draw do
resources :resumes, only: [:index, :new, :create, :destroy]
root "resumes#index"
get 'resumes/index'
get 'resumes/new'
get 'resumes/create'
get 'resumes/destroy'
Model-Resume.rb
class Resume < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.
validates :name, presence: true # Make sure the owner's name is present.
validates :attachment, presence: true
end
resume_controller.rb
class ResumesController < ApplicationController
def index
#resumes = Resume.all
end
def new
#resume = Resume.new
end
def create
#resume = Resume.new(resume_params)
if #resume.save
redirect_to resumes_path, notice: "The resume #{#resume.name} has been uploaded."
else
render "new"
end
end
def view
end
def destroy
#resume = Resume.find(params[:id])
#resume.destroy
redirect_to resumes_path, notice: "The resume #{#resume.name} has been deleted."
end
private
def resume_params
params.require(:resume).permit(:name, :attachment)
end
end
new.html.erb
<% if !#resume.errors.empty? %>
<div class = "alert alert-error">
<ul>
<% #resume.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class = "well">
<%= form_for #resume, html: { multipart: true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :attachment %>
<%= f.file_field :attachment %><br />
<%= f.submit "Save", class: "btn btn-primary" %>
<% end %>
</div>
index.html.erb
<% if !flash[:notice].blank?%>
<div class="alert alert-info">
<%= flash[:notice]%>
</div>
<%end%>
<br />
<%= link_to "New File", new_resume_path, class: "btn btn-primary"%>
<br />
<br />
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Download Link</th>
<th>View</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<% #resumes.each do|resume| %>
<tr>
<td><%= resume.name%></td>
<td><%= link_to "Download Resume", resume.attachment_url %></td>
<td><%= link_to "View "%></td>
<td><%= button_to "Delete", resume, method: :delete, class: "btn btn-danger", confirm: "Are you sure to delete #{resume.name}?" %></td>
</tr>
<% end %>
</tbody>
</table>
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.
Environment:
OS: Windows 8.1
Ruby: 2.1.5
Rails: 4.2
In my views/users/index.html.erb, I have:
<h1>Listing Users</h1>
<span class="eastdev_pagination">
<%= will_paginate #users, :container => false %>
</span>
<table class="table table-striped">
<thead>
<tr>
<th>First</th>
<th>Last</th>
<th>Work phone</th>
<th>City</th>
<th>Organization</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #users.each do |u| %>
<tr>
<td><%= u.first %></td>
<td><%= u.last %></td>
<% if u.work_phone %>
<% phone = number_to_phone(u.work_phone, area_code: true) %>
<td><%= phone %></td>
<% else %>
<td></td>
<% end %>
<td><%= u.city %></td>
<td><%= u.organization %></td>
<%= render partial: "layouts/show_edit_del_buttons", locals: {my_model: u} %>
</tr>
<% end %>
</tbody>
</table>
<span class="eastdev_pagination">
<%= will_paginate #users, :container => false %>
</span>
<br>
In my layouts/show_edit_del_buttons.html.erb, I have:
<td>
<%= link_to my_model, class: 'btn btn-info', title: 'View', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-search"></span>
<% end %>
<td>
<% if staff_access_level? %>
<td>
<%= link_to [:edit, my_model], class: 'btn btn-warning', title: 'Edit', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %>
</td>
<td>
<%= link_to my_model, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger', title: 'Delete', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
</td>
<% end %>
In my controllers/users.rb, I have the following:
def index
if params && params.has_key?('search-form')
item = params['search-form']
#users = User.where("first = ? OR last = ? OR organization LIKE ? AND approved = 1", item, item, "%#{item}%").paginate(:page => params[:page], per_page: 5).order('last')
else
#users = User.where("approved = 1").paginate(page: params[:page], per_page: 5)
end
end
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
The issue I am having, is that when I delete a user, I am returned to the first page. For example, I am on page 5 of the pagination, and I delete a user here, I am returned to the first navigation page.
How do I get the view to go back to the page I was on prior to the deletion?
Notes:
If I try something like:
<%= link_to my_model(:page => params[:page]), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger', title: 'Delete', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>>
I get an error message:
Method not found: my_model
You should redirect user after delete on page you were on.
Look at your destroy method
redirect_to users_url
Here you should define a page while redirect.
Like this
my_model = Your path, like user_path(user, page: params[:page])
<%= link_to user_path(user, page: params[:page]), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger', title: 'Delete', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
You will get current page number in side destroy method
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to params[:page] ? users_url(page: params[:page]) : users_url }
format.json { head :no_content }
end
end
I know there are a lot of issues with wkhtmltopdf and page breaks that date back years, but I haven't worked out a solution yet. I'm using the PDFKit gem to generate my html pages into pdfs, but I don't want the pages to break in the middle of a table row.
I'm using wkhtmltopdf-binary (0.9.9.3), which looks to be the most updated version
My CSS:
#media print {
#scores table tr td, #scores table tr th {
page-break-inside: avoid !important;
}
table, tr, td, th, tbody, thead, tfoot {
page-break-inside: avoid !important;
}
}
My table:
<div class="score_table">
<table id="scores" class="print-friendly">
<tbody>
<% #chapters.each do |chapter| %>
<tr>
<th colspan="3" ><%= chapter.name %></th>
</tr>
<% chapter.rules.each do |rule| %>
<tr>
<th colspan="2" >Rule: <%= rule.name %></th>
<th></th>
</tr>
<!-- Triggers -->
<% rule.triggers.each do |trigger| %>
<tr>
<td>T</td>
<td><%= markdown(trigger.body) %></td>
<td><%= markdown(trigger.explanation) %></td>
</tr>
<% if trigger.image? || trigger.image2? %>
<tr>
<td></td>
<% if trigger.image? %>
<td><%= image_tag trigger.image.url(:thumb) %></td>
<% else %>
<td></td>
<% end %>
<% if trigger.image2? %>
<td><%= image_tag trigger.image2.url(:thumb) %></td>
<% else %>
<td></td>
<% end %>
</tr>
<% end %>
<% end %>
<!-- Questions -->
<% rule.questions.each do |question| %>
<tr>
<td>Q</td>
<td><%= markdown(question.body) %></td>
<td><%= markdown(question.answer) %></td>
</tr>
<% if question.image? || question.image2? %>
<tr>
<td></td>
<% if question.image? %>
<td><%= image_tag question.image.url(:thumb) %></td>
<% else %>
<td></td>
<% end %>
<% if question.image2? %>
<td><%= image_tag question.image2.url(:thumb) %></td>
<% else %>
<td></td>
<% end %>
</tr>
<% end %>
<% end %>
<!-- Hints -->
<% rule.hints.each do |hint| %>
<tr>
<td>H</td>
<td><%= markdown(hint.body) %></td>
<td><%= markdown(hint.explanation) %></td>
</tr>
<% if hint.image? || hint.image2? %>
<tr>
<td></td>
<% if hint.image? %>
<td><%= image_tag hint.image.url(:thumb) %></td>
<% else %>
<td></td>
<% end %>
<% if hint.image2? %>
<td><%= image_tag hint.image2.url(:thumb) %></td>
<% else %>
<td></td>
<% end %>
</tr>
<% end %>
<% end %>
<% end %>
<% end %>
</tbody>
</table>
</div>
Is there a work around, or is there something that I'm doing wrong? This is the result:
I could post the PDFKit code as well, but it sounds like a wkhtmltopdf issue
***Update - My CSS #print isn't affecting the page when .pdf is added to the url. I have my stylesheet link with media: "all"
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
Here's my initializer pdfkit.rb:
ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}
If I can fix the CSS, then I probably will solve the page break issue!
i added this in css it worked for me
#media print {
#scores {
page-break-before: always;
}
}
I am using version wkhtmltopdf 0.12.0
For me, page breaks ONLY work with --print-media-type.
Without it, page break protection for images works, but not page-break-after or before.
I had to make a special css file for print media to get it work.
Setting the paper size to 'A3' or using the 'overflow: visible' didn't make any difference.
I ended up with the following:
in config/initializers:
PDFKit.configure do |config|
config.wkhtmltopdf = '/usr/bin/wkhtmltopdf'
config.default_options = {
:encoding=>"UTF-8",
:page_size => 'A3',
:zoom => 0.9,
:javascript_delay => 2000,
:print_media_type => true,
:footer_right => 'report, page [page] of [toPage]',
:no_background => true,
}
end
I am not 100% sure if the no_background is needed.
Then, in the controller:
def report_as_pdf
require 'nokogiri'
root_url = Rails.env.development? ? 'http://localhost:3000' : 'https://my.example.com'
html = render_to_string(layout: true, template: "reports/report_as_pdf").gsub('/assets', root_url + '/assets') # since wkhtmltopdf can not find the css files, we add the root_url to all assets
html_doc = Nokogiri::HTML(html)
html_doc.css('div.contain-to-grid').remove
html_doc.css('nav#rapportage_bar').remove
# Had to remove certain elements on the page. Not relevant for this particular problem, but it may help.
html = html_doc.to_html
filename = 'report_' + Time.now.to_s(:db)[0..9]
if Rails.env.development?
File.write("/tmp/#{filename}.html", html)
render :text => "wrote /tmp/#{filename}.html"
fork do
system("wkhtmltopdf --footer-right 'report, page [page] of [toPage]' --page-size 'A3' --zoom 0.9 --javascript-delay 2000 --no-background --print-media-type file:////tmp/#{filename}.html /tmp/#{filename}.pdf")
end
# forking was the only way I could get it to work in development mode, using thin as webserver instead of webrick (but it might work with webrick, I didn't try).
else
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => filename + '.pdf', :type => 'application/pdf')
# all is simpeler in production mode.
end
end
Note, that in the default layout (normally app/views/layouts/application.html.erb) I added one line to introduce a special css file for print:
stylesheet_link_tag "application_print", media: "print"
This file actually imports some tables from ZURB Foundation, which is cool.
#import "foundation/components/tables";
h1 {
font-variant: small-caps;
font-weight: bolder;
font-size: 320%;
border-bottom: 12px solid black;
margin-bottom: 20px;
margin-top: 80px;
}
h2 { (etc etc etc)
To make page breaks in a view, I just insert this in the right places:
I hope this will be useful to someone someday.
Can you upload the controller code where you've called PDFkit.new ?
Your stylesheet might not be included.
Try adding the stylesheet in your controller code like this:
def export
kit = PDFKit.new(render_to_string(layout: false, handlers: [:haml], formats: :html, template: 'score_tables/export'))
kit.stylesheets << "#{Rails.root}/vendor/assets/stylesheets/bootstrap.css"
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/score_tables.css"
kit
end
Here is how I fixed my issue.
- Updated PDFKIT to as of ( 01/22/2014 )
- I had to move my table out of the <div> as the nested table messes everything up.
Example:
No div tag wrapping the table.
From
<div class="score_table">
<table id="scores" class="print-friendly">
...rest of the HTML
To
<table id="scores" class="print-friendly">
...rest of the HTML
Hope this helps.
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")