Using Scaffold Form to Embed SoundCloud Link - ruby-on-rails

I'm trying to create a form in an app that takes a song's title, artist and soundcloud embed link so that these can be used in a post.
I created a scaffold, and set soundcloudembed:text
I added a link <%= post.soundcloudembed %> at the position where I want the embedded player to be displayed. This link does not work.
Does anyone know of a way to fix this?

Since your soundcloadembed field contains html tags you should tell rails to output raw data using one of these two methods:
<%== post.soundcloudembed %>
<%= post.soundcloudembed.html_safe %>

Related

Rails select opton using images

i have a select_tag in my rails form where i want to show images as option, i tried searching for a solution online but there is nothing quite good.
my form
<%= form.select :transport, ['Select an image', 'http://pngimg.com/uploads/tesla_car/tesla_car_PNG24.png', 'http://pluspng.com/img-png/png-hd-bike-ktm-duke-bike-png-download-1600.png' %>
i want something like this:
click here
Instead of struggling to let Rails handle it, It's recommended to use JQuery to populate options in the drop-down list.
This link guides you through the process to do it.
Click Here

Allow YouTube iFrame in Rails Application (saved in Textarea)

I have a Rails model called Posts and I want my users to be able to embed a YouTube video in the content section of their post.
The post model has fields: title (string) and content (textarea).
Right now they can add the YouTube embed code (iframe tag) to their content; however, the iframe does not appear in the show view for the post.
Why is this and what can I do to display the embedded video?
Thanks in advance.
Here's how I did it:
<%= simple_format(#post.content, {}, sanitize: false) %>
maybe you missed adding .html_safe
<%= #post.iframe.html_safe %>

How to add link field in migration?

I am trying to add a link field to a form. The link should be clickable when displayed. When I run
rails generate migration AddLinkToThings link:xxxx
What should xxx be? I've tried link:string, but that just puts out a string, of course.
Here is how I am making the title of a Thing link to where the user specifies:
<strong><%= link_to #thing.title, #thing.link %></strong>
The problem is that #thing.link is linking to the show page for the Thing, and not the link that the user typed in. It's ultra confusing to explain.
For example, when I created this Thing, I linked it to google.com, but instead, it's linking to mywebsite.com/things/7. Hope this makes sense.
It would be a string, but then you'd just use it inside of a link_to helper in your view.
Depending on the contents of the string, you may need to append .html_safe to the string inside the link_to tag.
Update
Based on your edit, I think you'll want to interpolate #thing.link inside a string.
Example:
<strong><%= link_to #thing.title, "#{#thing.link.try(:html_safe_}" %></strong>
If link will never be nil, you don't need the try.

Upload an image without displaying file_field and "apply" button. What's the correct way?

I am working in a rails 3.2 app,where the user has the avatar picture.
The user can chose his avatar in these way:
1) chose the file clicking the "Browse..." button in the edit user view (using file_field)
2) chose the file and click OK
3) then click on the "Apply Photo" button (on the right of the field containing the filename) to load the image to the server and update the view
I want improve the user experience, removing the field and the button in the view. This should be the way:
1) click a link 'Update avatar' (or directly click on the picture)
2) chose the file and click OK
No (visible) fields and no Apply button
What's the best way to do that ?
should I continue to use the file_field in the view but hide it and trigger the user#update action using some javascript code ? or is there some other better way to do that ?
Thank You
You can use one of js plugins to upload files. I.e. if you are using jQuery, you can try using plupload
I have encountered this issue with hiding the input element and putting a label instead.
For eliminating submit button, I have added onchange method.
<%= file_field_tag :file, class: "d-none", onchange: "this.form.submit()" %>
<%= label_tag :file, role: "button" do %>
<%= image_pack_tag('logo.svg') %>
<% end %>
PS: I'm using bootstrap class here, styles can be used instead for hiding the element.
The button near upload field is generated by browser and is standart widget for browser. You can use js plugin for that or realize iframe upload method (allow you async file upload even on IE). Simple tutorial for iframe - there Iframe method allow to you to customize upload link/field as you want =)

Rendering field data as a link in Ruby on Rails

Ok, I think this is probably an easy question but for the life of my I can't figure it out. I have created a table called ugtags and in that table I have two columns (beyond the basics), 'name' and 'link'.
I am trying to allow a user to add a link to a page. Ideally they would enter the link title (name) and the url (link) and in the view it would display the title as a link to the url that was entered in the link column.
I there a way to do it by simply affecting the <%= link_to h(ugtag.name) %> code?
You should just be able to do:
<%= link_to h(ugtag.name), ugtag.link %>
See the documentation for all of the relevant options.

Resources