How to display images with CarrierWave in Sinatra - sinatra

I have struggled a lot with CarrierWave, so I made a small program to see if I could make that work:
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'carrierwave/datamapper'
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
end
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/photogallery.sqlite3")
class Image
include DataMapper::Resource
property :id, Serial
property :title, Text
mount_uploader :image, ImageUploader
end
DataMapper.finalize
DataMapper.auto_migrate!
get "/" do
#images = Image.all
erb :index
end
post "/" do
image = Image.new(:title => params[:title])
image.image = params[:image]
image.save
redirect "/"
end
__END__
## index
<% #images.each do |image| %>
<p>
<%= image.title %>
</p>
<a href="<%= image.image.url %>">
<img src="<%= image.image.url %>">
</a>
<% end %>
<form action="/" method="post">
<label for="title">Title</label>
<input type="text" name="title"><br>
<label for="image">Image</label>
<input type="file" name="image"><br>
<input type="submit" value="Save">
</form>
It seems that I am unable to display the image when I upload an image from my computer. It is as if the images are not saved anywhere, so they can't be retrieved.
The anchor tag correctly references where the images should be, but there are no images.
What am I doing wrong?

Converted to an answer for #MrMos...
Nevermind, I found out. I did this instead:
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'carrierwave/datamapper'
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
'images'
end
end
class Image
include DataMapper::Resource
property :id, Serial
property :title, Text
mount_uploader :file, ImageUploader
end
DataMapper.finalize
DataMapper.auto_migrate!
get '/' do
#images = Image.all
erb :index
end
post '/' do
Image.create(params[:image])
redirect to('/')
end
__END__
## index
<!DOCTYPE html>
<html>
<body>
<form action="/" method="post" enctype="multipart/form-data"></div>
<input type="text" name="image[title]" />
<input type="file" name="image[file]" />
<input type="submit" name="submit" value="Upload" />
</form>
<% #images.each do |image| %>
<p><%= image.title %></p>
<img src="/images/<%= images.file_identifier %>" />
<% end %>
</body>
</html>

Related

SyntaxError: missing ) after argument list in index.ejs while compiling ejs - although correct?

<header>
<% include templates/header.ejs %>
</header>
<% include templates/announcement.ejs %>
<form>
<br><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search a fort...">
</form><br>
<section id="placeList">
<div class="placeListBackground" id="placeListGet">
<ul id="placeCells">
<% placeData.forEach(function(placeData) { %>
<div class="placeContainer <%= placeData.category %>" class><img src= "<%= placeData.placeicon %>" alt="<%= placeData.placename %>" align="left" width="178" height="100"><span><%= placeData.placename %></span><p><%= placeData.clan %></p><p>Playing: <%= placeData.playing %></p><p class="sCategory"><%= placeData.category %></p></div>
<% }); %>
</ul>
</div>
</section>
I get an error saying "SyntaxError: missing ) after argument list in index.ejs while compiling ejs". Thing is, on my other computer this is exactly the same written code and works perfectly fine. Is it an issue with ejs or is there something I'm missing that may be significant?
You should try to following suggestion. It works for me so it should work for you.
<%- include('./templates/header.ejs');%>
<%- include('./templates/announcement.ejs');%>

Is it possible to call another parital to the harpe js parameter?

I'm harp js user and im curious if its possible to call another parital to the harp js parameter?
I would like to proceed with modularization to reuse the code.
It is difficult to use the parital function of harp to insert data into a module of a particular module into another module.
This is my sources:
//component/button.ejs
<button><%= lable %></button>
//component/tab.ejs
<div class="tab">
<div class="tab__nav">
<% for(var i in locals.payload){ %>
<div class="tab__nav-item"><%- locals.payload[i].name %></div>
<% }; %>
</div>
<div class="tab__content">
<% for(var i in locals.payload){ %>
<div class="tab__content-item">
<div class="container">
<%- locals.payload[i].content %>
</div>
</div>
<% }; %>
</div>
</div>
//page.ejs
<%- partial("component/tab",{
payload: [
{
name:'tab1',
content:`
<div><%= partial("component/button.ejs",{label:"btn"})%></div>
`
},
{
name:'tab2',
content: `
<div>111111</div>
`
}
]}) %>

Rails 4 ActionMailer Create and send email from form

I am new to actionmailer and can't seem to get ActionMailer to work and route properly, not sure if I'm doing it properly and most tutorials only show how to generate a generic email after a user has first created an account.
What I am trying to do is allow a user to generate an email through a form to send an email message with a link to a view to their clients to view an invoice.
My current error is due to some routing issues but the strange thing was that it's looking for an email template in my controller instead of the mailer controller is my assumption... Missing template invoices/send_invoice_email, application/send_invoice_email
Here is my form which the user would use to build the email.
<%= bootstrap_form_tag url: '/send_invoice' do |l| %>
<%= l.hidden_field :invoice, value: #quote.id %>
<div class="input-group margin-bottom-20">
<span class="input-group-addon" id="from"><i class="fa fa-envelope"> </i></span>
<%= l.text_field :from, hide_label: true, value: current_user.email %>
</div>
<div class="input-group margin-bottom-20">
<span class="input-group-addon" id="to"><i class="fa fa-user"> </i></span>
<%= l.text_field :to, hide_label: true, value: #quote.client.contacts.first.email %>
</div>
<div class="input-group margin-bottom-20">
<span class="input-group-addon" id="cc"><i class="fa fa-users"> </i></span>
<%= l.text_field :cc, hide_label: true, placeholder: "cc:" %>
</div>
<div class="input-group margin-bottom-20">
<span class="input-group-addon" id="bcc"><i class="fa fa-users"> </i></span>
<%= l.text_field :bcc, hide_label: true, placeholder: "bcc:" %>
</div>
<div class="input-group margin-bottom-20">
<span class="input-group-addon" id="subject"> <i class="fa fa-bullhorn"></i></span>
<%= l.text_field :subject, hide_label: true, placeholder: "Subject" %>
</div>
<div class="input-group margin-bottom-20">
<span class="input-group-addon" id="message"> <i class="fa fa-comment"></i></span>
<%= l.text_area :message, hide_label: true, placeholder: "Message", rows:"5" %>
</div>
<%= l.submit "Send", class:'btn-u btn-u-blue btn-block' %>
<% end %>
My controller method in my invoice controller (this is only a show controller, Quote is created in quote model)
def send_invoice_email
quote = Quote.find(params[:invoice])
InvoiceMailer.send_invoice_email(quote, params)
end
And here is the method in my invoice mailer
def send_invoice_email(quote, params)
#quote = quote
mail(
to: current_user.email,
from: params[:from],
content_type: "text/html",
body: params[:body],
content_type: "text/html",
subject: params[:subject],
content_type: "text/html"
)
end
I changed a few things and now it seems to be working... Not sure why but I'll take it. Here are my current configurations..
Form is the same.
In my invoice controller I now have
def send_invoice_email
quote = Quote.find(params[:invoice])
InvoiceMailer.invoice_email(quote, params).deliver_now
end
In my invoice mailer I now have
def invoice_email(quote, params)
#user = quote.user
#quote = quote
#message = params[:body]
mail to: params[:to],
from: #user.email,
body: #message,
subject: params[:subject],
content_type: "text/html"
end

Ruby On Rails "undefined method `description' for #" when save

I'm relatively new to rails and I've been struggling with this for a couple of days. I'd be much appreciated if you can see where I've gone wrong.
I got this error message
undefined method `description' for #<Invoicegeneric:0x000001054900b0>
if #invoicegeneric.save ==> error at this
response = "Success"
else
response = "Error"
and this is my controller code
def addgenericitems
#invoicegeneric = Invoicegeneric.new
#invoicegeneric.invoice_id = params[:invoice_id]
#invoicegeneric.po_id = params[:po_id]
if #invoicegeneric.save
response = "Success"
else
response = "Error"
end
render :json => {:html => render_to_string(:partial => "invoices/invoicegenerics", :layout => false) }.to_json, :status => :ok
end
and this is my html
<form id="modal-form" class="pure-form pure-form-stacked"
name="invoicegenericform" action="<%= addgenericitems_invoice_path(#invoice) %>"
method="post">
<fieldset>
<label for="po_id">PO</label>
<select name="po_id" class="chzn-select">
<% Purchaseorder.active.order(:sku).each do |p| %>
<option value="<%= p.sku %>"><%= "#{p.sku}" %></option>
<% end %>
</select>
</fieldset>
<fieldset>
<label for="sku_sj">SJ</label>
<input name="sku_sj" type="text">
</fieldset>
<div class="modal-footer">
<button class="pure-button" data-dismiss="modal" aria-hidden="true">Close</button>
<input type="submit" class="pure-button pure-button-primary" value="Tambah"/>
</div>
</form>
In my table there is no column name description

Liferay Form post activated only on clicking the submit button 2 times?

I'm posting password via Liferay form and storing it in session called "password",
when I'm putting the correct password which I made it as a default for now "1234" the session gets activated
and when I browse across the pages it's working fine.
But when I'm posting it for the first time, it doesn't work, I need to click 2 times on the button so I can see the data or if I go to another page.
Can anyone here support me in this. This is my full code:
<%
String value = BeanParamUtil.getString(article, request, "structureId");
%>
<portlet:actionURL secure="<%= PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS || request.isSecure() %>" var="SecondloginURL">
<portlet:param name="saveLastPath" value="0" />
<portlet:param name="struts_action" value="/journal_content/view" />
</portlet:actionURL>
<c:choose>
<c:when test="<%= value.equals(\"10801\") %>">
<%
HttpSession session1 = request.getSession(false);
out.print(session1 + "<br>");
String sessionId = session1.getId();
out.print(sessionId + "<br>");
String foo = (String) session1.getAttribute("password");
out.print(foo + "<br>");
%>
<c:choose>
<c:when test="<%= !Validator.isNull(foo) %>">
<h2>this is the second password and it's working</h2>
<div class="journal-content-article"
id="article_<%= articleDisplay.getCompanyId() %>_<%= articleDisplay.getGroupId() %>_<%= articleDisplay.getArticleId() %>_<%= articleDisplay.getVersion() %>">
<%= RuntimePortletUtil.processXML(application, request, response, renderRequest, renderResponse, articleDisplay.getContent()) %>
</div>
</c:when>
<c:otherwise>
<aui:form action="<%= SecondloginURL %>" name="auth" method="POST">
<aui:input label="Second Password" type="password" name="password" />
<aui:button type="submit" value="authenticate" onClick="location.reload(true)" />
</aui:form>
<%
String pass = request.getParameter("password");
out.println(pass+" = 1234");
%>
<c:if test="<%= Validator.equals(pass, \"1234\") %>">
<%
session1.setAttribute("password","authenticated");
%>
</c:if>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<div class="journal-content-article"
id="article_<%= articleDisplay.getCompanyId() %>_<%= articleDisplay.getGroupId() %>_<%= articleDisplay.getArticleId() %>_<%= articleDisplay.getVersion() %>">
<%= RuntimePortletUtil.processXML(application, request, response, renderRequest, renderResponse, articleDisplay.getContent()) %>
</div>
</c:otherwise>
</c:choose>
I'm doing a hook on the journal_content/view
What is happening is when the page is refreshed for the first time after (using location.reload()) the session is has not been set till the time you are checking for it and it is being set in the <otherwise> block below the condition.
So when you click the second time to reload the session has already set. Even if you just refresh the page the second time without clicking on the button you will find that the session is set.
So one way I can think of is to use struts-action hook (if you are using Liferay 6.1) to move the setting of the session code in the respective View-action class.
Or else
Can try to move the setting of the session code before you check for the foo string.