Using HTML in A Form - html

I am trying to create a form that accepts HTML. So that when I type <br> or use divs it understands and populated my content block accordingly. Can anyone point me in the right direction for this?
Here's the form I am using if that helps:
<%= simple_form_for(#daily) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :content %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>

Replace <%= f.input :content %> with <%= f.text_area :content %>. That doesn't do much but make a multi-line input area (looks better). However, when you display the data on your webpage (i.e. after you submit the form), you can put something like this:
<p>#daily.content.html_safe</p>

Related

Submitting multiple forms in one click

I was looking at the question below, which has a good answer for what I will ask, but I have a doubt. I cannot comment yet so that is why I am making another post.
How to submit multiple, duplicate forms from same page in Rails - preferably with one button
This is my view for the form
new.html.erb
<h1 class="page-title">Nuevo Precio</h1>
<hr class="title-division">
<div class="alta-form-container">
<%= form_tag "/precios" do %>
<% 2.times do %>
<%= render 'form', precio: #precio %>
<% end %>
<%= submit_tag "Submit", class: "btn btn-success" %>
<% end %>
<%= link_to 'Lista Precios Cliente', client_precios_path %> <br><%= link_to 'Ver', [#client, #precio] %> <br>
</div>
and my _form.html.erb
<div class="field">
<%= label_tag :precio, "Precio" %>
<%= text_field_tag "precios[][precio]" %>
</div>
<div class="field">
<%= text_field_tag "precios[][cant_acomodato]" %>
</div>
<div class="field">
<%= hidden_field_tag "precios[][client_id]" %>
</div>
When I submit the form I get an error that says
'No route matches [POST] "/precios"'
which I'm guessing is because on my new.html.erb I wrote form_tag "/precios" do
Any thoughts on what I should change or edit? thanks in advance
actually you have only one form here, because _form.html.erb will generate only inputs, that according to form below
<%= form_tag "/precios" do %>
that will generate
form action="/precios" method="post">
if you want leave it like that, you should add to your config/routes.rb
post '/precios', to: 'controller_name#method_name', as: :controller_name_method_name
your situation:
post '/precios', to: 'PreciosController#create', as: :precios_create
also check this
documentation

How can I use form_for to display a text_field that cannot be edited?

I'm trying to set up a form on one of my pages that lists some info that should not be editable. My code for the page looks like this:
<%= form_for #quote do |f| %>
<h2 class='h2Title'>Follow Up Popup</h2>
<div class="field" id="msQuoteNumber">
<%= f.label "Quote Number:" %>
<%= f.text_field :quote_number %>
</div>
<% end %>
My page looks like this:
Right now I'm using f.text_field :quote_number to fill in the field with the quote_number from my database. This however lets the user type whatever they want into that field which I don't want. Is there a different method I can use besides text_field that just simply shows the quote_number as regular text?
To just display the input field and disable only in case of editing:
<%= f.text_field :quote_number, disabled: !(#quote.new_record?) %>
In Controller, just remove the key :quote_number from permitted attributes in case if you are editing so that no one can update the quote number after changing it via Inspect Element functionality of the browser and then submitting the form.
Use a label like the line above
Was:
<%= f.label "Quote Number:" %>
<%= f.text_field :quote_number %>
Change to:
<%= f.label "Quote Number:" %>
<%= f.label :quote_number %>

Bootstrap Grid Alignment Issues

So I'm following a tutorial for RoR but I can't seem to get the alignment right.If I copy and paste the code for the tutorial, it will look like
this
Here is the code:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
But if I put col-md-1, then it sort of works, looking like this
Is it possible to still use col-md-6 but have it properly aligned like the second photo? I don't know what else to change other than the numbers.
I am not able to test this right now but you may be able to change your first line of your form to:
<%= form_for #user, :html => {:class => "form-horizontal"} do |f| %>
This should add the html and css need to make the form horizontal:
<form class="form-horizontal">
Some what like this example: https://jsfiddle.net/obp9x1yr/7/
This might be a good post to read for more information: Rails Bootstrap how to format form_for (width grid collapses)

Rails: creating a drop down menu for a form

Would you know how to make a pull down or drop down menu for a form text field?
Currently, the user needs to to type in text "Rating" then submit the form. But ideally, they should select a rating of "Awesome" "Good" or "Meh" from a pull down, rather than be able to enter a custom text. Any advice in the right direction would be appreciated.
Thank you.
_form.html.erb
<%= simple_form_for(#post) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :Duration, required: true, autofocus: true %>
<%= f.input :Rating %>
</div>
<div class="form-actions">
<%= f.button :submit, "Log it!" %>
</div>
<% end %>
Please try to replace the line:
<%= f.input :Rating %>
with:
<%= f.select :Rating, options_for_select(["Awesome", "Good", "Meh"]) %>

HTML Script Evaluations

Disclaimer: I'm still very new to web development in general, so I'm sorry if this is a terrible question.
Why is everything in HTML evaluated on a line-by-line basis? I've noticed that any code (scripts?) that I throw into HTML requires me to cast it to its language on every single line like so:
<div class = "row">
<div class = "span6 offset3">
<%= form_for(#user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.text_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.text_field :password_confirmation %>
<%= f.submit "Create My Account", :class => "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
But why can't it evaluate the scripts like this:
<div class = "row">
<div class = "span6 offset3">
<%= form_for(#user) do |f|
f.label :name
f.text_field :name
f.label :email
f.text_field :email
f.label :password
f.text_field :password
f.label :password_confirmation, "Confirmation"
f.text_field :password_confirmation
f.submit "Create My Account", :class => "btn btn-large btn-primary"
end %>
</div>
</div>
Would it not make developing so much simpler to allow for this? To me, this just makes so much more sense, but its not done this way, probably for a good reason. If I'm writing out a fair bit of code (Ruby in this case), I am not a huge fan of casting every line at the start and finish.
I get that I'm having the HTML evaluate each line as an HTML object by casting it, but is it not possible to just have it assume that every line (or some other syntax - maybe actually use semi-colons) is a new piece of script to evaluate?
I guess what I'm asking is why are blocks of code evaluated this way and why hasn't it been made more efficient?
You can, but you have to do everything that the <%= %> tag does.
I'm not sure about the syntax in Ruby, but basically a tag like this:
<%= f.label :name %>
does output to the page, i.e. a shortcut for:
<% response.write(f.label :name) %>
So, you could do it it within a single script tag, if you add the commands for doing the output:
<%= form_for(#user) do |f|
response.write(f.label :name)
response.write(f.text_field :name)
et.c...