update `link_to` helper when clicked; Rails 4 - ruby-on-rails-4

In a view.html.erb, I want to use something like:
<tbody>
<% #books.each do |book| %>
<tr>
<td><%= book.title %></td>
<td><%= book.author %></td>
<td><%= link_to 'Show', book, class: 'btn btn-mini btn-primary' %></td>
<td><%= link_to 'Edit', edit_book_path(book), class: 'btn btn-default' %></td>
<td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-mini btn-danger' %></td>
<td><%= link_to 'Reserve it', books_path(book, :situation_id => 2), class: "btn btn-mini btn-success" %></td>
</tr>
<% end %>
</tbody>
When I click on the button "Reserve it", I would like that the field "situation_id" of this book become equal to 2.
How can I do that?

If a book has a situation association (and your associations are set up correctly), then you could pass it as a parameter with <%= link_to 'Reserve it', books_path(book, :situation_id => book.situation_id), ... %>
However, you shouldn't need to pass it the situation_id param since you can just grab it with the book instance variable that you're going to. In other words you can do
def show
#book = Book.find(params[:id]).include(:situation)
end
and your book will load up the situation as well and it will be available in your book show view.

Not a Rails solution but I hope this will help you thinking in right direction may be.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#link").click(function(){
$("a").attr("href", function(i, href) {
return href + 'books/?situation_id=2';
});
});
});
</script>
</head>
<body>
<a href="" id="link" >hello</a>
</body>
</html>

Related

Routing Error No route matches [POST] "/resumes/58"

I'm using Rails 4.2.6 with an sqlite3 database. I want to print the data from database into a file which is readable. But when I click the "Show" button it gives me this error even though I haven't set show's route to POST.
Here is the stuff am working with:
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></th>
</tr>
</thead>
<tbody>
<% #resumes.each do|resume| %>
<tr>
<td><%= resume.name%></td>
<td><%= link_to "Download Resume", resume.attachment_url %></td>
<td><%= button_to "Delete", resume, method: :delete, class: "btn btn-danger", confirm: "Are you sure to delete #{resume.name}?" %></td>
<td><%= button_to "Show", resume, method: :show, class: "btn btn-primary"%></td>
</tr>
<% end %>
</tbody>
</table>
routes.rb:
Rails.application.routes.draw do
resources :resumes
root "resumes#index"
get 'resumes/index'
get 'resumes/new'
get 'resumes/create'
get 'resumes/destroy'
get 'resumes/show'
end
rake routes:
Prefix Verb URI Pattern Controller#Action
resumes GET /resumes(.:format) resumes#index
POST /resumes(.:format) resumes#create
new_resume GET /resumes/new(.:format) resumes#new
edit_resume GET /resumes/:id/edit(.:format) resumes#edit
resume GET /resumes/:id(.:format) resumes#show
PATCH /resumes/:id(.:format) resumes#update
PUT /resumes/:id(.:format) resumes#update
DELETE /resumes/:id(.:format) resumes#destroy
root GET / resumes#index
resumes_index GET /resumes/index(.:format) resumes#index
resumes_new GET /resumes/new(.:format) resumes#new
resumes_create GET /resumes/create(.:format) resumes#create
resumes_destroy GET /resumes/destroy(.:format) resumes#destroy
resumes_show GET /resumes/show(.:format) resumes#show
Please tell me if anything else is also needed.
Try updating the show button syntax line:
<td><%= button_to "Show", url_for(controller: 'resumes', action: 'show', id: resume.id), method: :get, class: "btn btn-primary"%></td>
Also, here is a nice little description of the different HTTP method and their related Rails actions.

How to view and edit the uploaded excel sheets in rails using carrierwave gem (win7)

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>

Returning view to page I started from when deleting a user in a Rails view

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

WKHTMLTOPDF with pdfkit on Rails ignoring table page breaks

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.

haml to html translation

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>