Can I build a model with Paperclip attachment? - ruby-on-rails

I have a model Style which has a paperclip attachment file. I am using a multi-page form for creating the Styles. In my first step I upload the file and in the next page I update the other attributes of the Style record. I don't know how to build the style with the attachment so that it can be used in the later step.
Below is my action for my second step. The attachment file is not yet saved, so how do I build it that the file is available in the view. Any help is appreciated.
def build_form
styles = params_array.delete(:styles)
#styles = []
styles.each do |style_file|
name = style_file.original_filename.split(".")[0..-2].join(".")
#styles << Style.new(name: name, file: style_file)
end
render :new
end

HTTP is stateless so if your multipage form is using HTTP requests I think you should be able to save it to database or store in the session.
If you are saving it to the database you should remove record at the end if any step is not valid.
But I think multipage form should use js, so file content and file info is saved to the form.
So my suggestion is to use ajax for multipage form, in this case, you will have all content on the page, but some content would be hidden

Related

convert SVG code to format to attach to user in Rails

Essentially, in my app users can create a custom svg element using js (stimulus).
My user.rb model has:
has_one_attached :picture
in my attach_image_controller.js I can access the svg code:
<svg>
<-- blah blah >
</svg>
What is the best way to transform this code into a format that can be attached in my user_controller.rb?
I have tried turning the code into a blob and creating an object url, but I haven't been able to then attach it to a user (see below attempt)
attach_image_controller.js
var testsvg = `<svg>
<-- blah blah >
</svg>`;
var blob = new Blob([testsvg], {type: "image/svg+xml"});
var url = URL.createObjectURL(blob);
console.log(url); // returns: blob:http://localhost:3000/15107c0d-b794-4be6-a1e0-2cf9e8f49174 and results in routing error
// As there is a routing error, this url can't be used as per below
and then when passed to the user_controller.rb:
user_controller.rb
#url = params[:url]
#user = current_user
file = URI.open(#url[5..-1])
#user.picture.attach(
io: file,
filename: #url[5..-1],
content_type: 'image/svg+xml')
Edit:
To make things more complicated, in some cases, users have the ability to upload and attach an image, or generate a svg code that renders in the DOM, but I want to be able to attach both the SVG code and the uploaded image.
The typical use case for attachments is when we wish to associate an uploaded file (often an image, e.g. jpg) with a model.
However the svg is an html snippet, it's a string comprised of a single html element (<svg>... </svg>). So you might consider simply including it as a string column in your User model. Remember, though, that you need to declare it html_safe before it will render correctly. So if you are using erb, you'll have <%= user.svg_image.html_safe %>.

Why does an Action Text attachment render out an "action-text-attachment" tag when rendered as HTML?

I understand that Action Text attachments are store in the DB in a compressed form that appears as an "action-text-attachment" tag. However when rendered using to_trix_html this tag is not rendered as I suppose it would mess with Trix's internal model.
I cannot understand why this tag is required when Rich Text is rendered as HTML (for example in a show action). I'd really appreciate if someone could explain why this has been designed this way.
its need to be there as an identifier and its saved like that inside the database
the tag contain 5 attributes content-type
url
filename
filesize and sgid
sgid is Signed Global IDs its unique to each file the function is as an anti-tamper and identifier about the attached file
as for the .to_trix_html giving different tag yes its need to be like that
because we want different way to handle the attachment inside the trix editor and when it outside the trix editor.
and if you want to know more about how the attachments works in action_text you could check this blog post

Rails scraping - submitting a form

I am filling in a form on a page and submitting it.
This should trigger the download of a file.
However, when I try to save the output of the download, I get the source code of the page rather than the file.
My code is:
mechanize = Mechanize.new
mechanize.pluggable_parser.default = Mechanize::Download
page = mechanize.get('http://page.com/')
form = page.forms.first
form.radiobuttons_with(name: 'presence')[0].check
form.source = "btce"
form.label = "BTC/USD"
mechanize.get_file(form.submit).save!('page.csv')
How can I save a file which is downloaded when I submit a form?
Does the file automatically begin downloading once you submit the form?
Submitting a form may return a new page, also new scripts/stylesheets can be loaded. Which possibly explains why your file contains the source code since that's what you're downloading. (Mechanize doesn't throw an error if you download a web page)
For example, I use Mechanize to fill out Google's search form and submit it and save the results to google_search.csv. The new file contains a mixture of the page's source code along with javascript, mySQL, and its stylesheets.
You can dig through the page's source code using Firebug and pinpoint what exactly happens when you submit the form, which can likely be a link that's invoked but you were unaware of.

How do I convert a page of HTML to a page on my Rails site?

I added an upload form so people can upload HTML files to my site. How do I parse a file of HTML to create a page of content on the site? Currently, I just need to get the title and body of a file, so I thought a full-blown parser like Nokogiri would be overkill.
#this takes in a <ActionDispatch::Http::UploadedFile>
def import(file)
#code to get title and body?
end
One of many ways to do this..
You can open and read the file from your controller assuming you saved it to an object somewhere.
#content = File.read(#your_saved_object.attachment.file_name)
And then in your view ( in Haml ) :
#content-container= #content

Rails validation

I am suffering a issue with rails server side validation. Can some one help me out from this?
situation is :
I am creating dynamic for and its elements also dynamic.My application will generate the some HTMl code. Which can we use in any form or blog..
I applying the server side validation. But due to dynamic elements .I am not able to store the last entered value in to the elements. AS we normally does in PHP if user input something wrong we don't put the field empty. So I need to find a mechanism which fills the older values into the elements,If something went wrong.
This is the code into controller which is I'm using to show the form :
render :layout => false,:template=>'buildders/rander_form'
and view of rander_form.html.erb has
<%= render :file=>RAILS_ROOT+'/public/forms/form_'+#form_name+'.html.erb' %>
where #form_name is a dynamic form name(which have HTML code).
Can some one help me?
don't put erb files in public, people can download them by entering the file path in the url
also why not move that code out of the erb template into the controller?

Resources