How do I make rails form responsive to a hyperlink?

Bill K

I am following Michael Hartl's Rails Tutorial and have completed the part about creating microposts. I was wondering if anyone have an idea about how to make the micropost form responsive to a hyperlink. For example, when a user types in "<a href="http://www.w3schools.com/html/">Visit our HTML tutorial</a>" in the micropost, I want the link to active. Any help would be appreciated.

micropost_controller.rb

class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy


def create
  @micropost = current_user.microposts.build(micropost_params)
  if @micropost.save
  flash[:success] = "Micropost created!"
  redirect_to root_url
else
  @feed_items = []
  render 'static_pages/home'
end
end

def destroy
  @micropost.destroy
  redirect_to root_url
end

private 

  def micropost_params
    params.require(:micropost).permit(:html)
end

def correct_user
  @micropost = current_user.microposts.find_by(id: params[:id])
  redirect_to root_url if @micropost.nil?
end
end

micropost.rb

 class Micropost < ActiveRecord::Base   
   belongs_to :user  
   default_scope -> { order('created_at DESC') }   validates :content,
   presence: true, length: { maximum: 140 }   validates :user_id,
   presence: true end

 ...
 end

micropost_form.html.erb

<%= form_for(@micropost) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :content, placeholder: "Compose new micropost..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
JTG

You can use the sanitize helper method and pass in the anchor (a) tag as the only allowable tag. You don't use it when they create the post, you use it when you are showing the micropost in the view

app/views/microposts/show.html.erb

<%= sanitize micropost.content, tags: ['a'] %>

(I don't know exactly how you are showing the content of a micropost, but this should give you an idea) This is safer than other options like html_safe because you can actually control which html tags you will allow the user to be able to input.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I make rails form responsive to a hyperlink?

From Dev

How do I make this code more responsive?

From Dev

How do I make this responsive layout with CSS?

From Dev

How do I make a background image responsive?

From Dev

How do I make a website responsive?

From Dev

How do I make this right content responsive?

From Dev

How do i make the sidebar responsive

From Dev

How do I create a form in angular that merges a hyperlink to a string?

From Dev

How do I get a hyperlink to show up in a form in microsoft access?

From Dev

How can I make my form responsive on all sizes

From Dev

How do I make a Pinterest Widget Builder Responsive?

From Java

How do I make the navbar responsive depending on the viewport?

From Java

How do I make my absolute-positioned elements responsive?

From Dev

How do I make semantic ui pages responsive?

From Dev

How do I make my website's background image responsive?

From Dev

how do i make this responsive nav bar work properly?

From Dev

How do I make semantic ui pages responsive?

From Dev

How do I make a responsive div with scroll content in HTML?

From Dev

How do i make a form with an image and text

From Dev

How can I make a hyperlink in kivy?

From Dev

How to make some things not wrap in responsive form?

From Dev

How to make css search form responsive

From Dev

How to make a form input width to responsive?

From Dev

How to make inline form elements responsive

From Dev

What could I do to make this menu responsive?

From Dev

How do I make a hyperlink within a Word document to another place in the same document?

From Dev

How do I make hyperlink move to next page taking angularjs variable with it?

From Dev

Spring-boot Thymeleaf: How do I display an entity name from the database and make that name a hyperlink?

From Dev

How can I make responsive html table fields which are automatically filled in by user's input in an html form?

Related Related

  1. 1

    How do I make rails form responsive to a hyperlink?

  2. 2

    How do I make this code more responsive?

  3. 3

    How do I make this responsive layout with CSS?

  4. 4

    How do I make a background image responsive?

  5. 5

    How do I make a website responsive?

  6. 6

    How do I make this right content responsive?

  7. 7

    How do i make the sidebar responsive

  8. 8

    How do I create a form in angular that merges a hyperlink to a string?

  9. 9

    How do I get a hyperlink to show up in a form in microsoft access?

  10. 10

    How can I make my form responsive on all sizes

  11. 11

    How do I make a Pinterest Widget Builder Responsive?

  12. 12

    How do I make the navbar responsive depending on the viewport?

  13. 13

    How do I make my absolute-positioned elements responsive?

  14. 14

    How do I make semantic ui pages responsive?

  15. 15

    How do I make my website's background image responsive?

  16. 16

    how do i make this responsive nav bar work properly?

  17. 17

    How do I make semantic ui pages responsive?

  18. 18

    How do I make a responsive div with scroll content in HTML?

  19. 19

    How do i make a form with an image and text

  20. 20

    How can I make a hyperlink in kivy?

  21. 21

    How to make some things not wrap in responsive form?

  22. 22

    How to make css search form responsive

  23. 23

    How to make a form input width to responsive?

  24. 24

    How to make inline form elements responsive

  25. 25

    What could I do to make this menu responsive?

  26. 26

    How do I make a hyperlink within a Word document to another place in the same document?

  27. 27

    How do I make hyperlink move to next page taking angularjs variable with it?

  28. 28

    Spring-boot Thymeleaf: How do I display an entity name from the database and make that name a hyperlink?

  29. 29

    How can I make responsive html table fields which are automatically filled in by user's input in an html form?

HotTag

Archive