Remove newline in textarea generated by HAML / Form Builder - html

I have a very simple Rails form.:
= form_for #object :remote => true do |form|
= form.text_area :text, :class => 'form-control'
For a new object, with a nil text attribute, this generates:
<textarea class="form-control" name="object[text]" id="object_text"> </textarea>
The blank space in that is a newline:
(byebug) form.text_area :text, :class => 'form-control'
"<textarea class=\"form-control\" name=\"user_deactivation[reason_text]\" id=\"user_deactivation_reason_text\">\n</textarea>"
I strip out the leading and trailing spaces on save, so data wise, its not a big deal, but when the user clicks on this field, it appears indented.
Relevant software versions:
Rails 5.02
haml-4.0.7
The text area is in a bootstrap 3 dialog
Why is this newline being generated and how can I stop it?

You can use the '~' operator, which is like the '=' operator, but automatically runs find_and_preserve on the output.
Like this:
= form_for #object :remote => true do |form|
~ form.text_area :text, :class => 'form-control'

This is not an ideal answer, but it does work. I would like something better:
= find_and_preserve(form.text_area :text, :class => 'form-control')
Can that be automatic? Is there a way to have the text_area helper not insert a newline?

Related

Simple_form_for form tag

I'm having weird issue, while simple form not creating  <form> tag, but creates all inputs
My code looks like:
= simple_form_for #cost, url: cost_url, html: {remote:true, id: 'new_cost_form'} do |f|
= f.input :name, placeholder: "Cost name"
= f.input :factor, as: :text, :input_html => { 'rows' => 5}
= f.input :is_default, as: :boolean, label: "Default"
And this code produces me single inputs(with proper names) but there is no html <form> tag on page(i've tried to exec in browser console $('#new_cost_form') and there was empty array
Did i miss something? I've created lots of forms this way, but only this didn't work.

Pulling values from forms text fields to other locations in ruby on rails html

I am new to ruby on rails and am composing a library for books. Most of it is working except for moving a scanned in isbn from a text field on a page to another text field.
<%= form_for #book, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :isbn, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :isbn, :class => 'text_field' %>
<%= link_to t('.new_autofill', :default => t("helpers.links.Auto Fill")),
autofill_books_path( :isbnvalue => :isbn ),
:class => 'btn btn-primary' %>
</div>
</div>
<div class="control-group">
The code above takes the value isbn and passes it into the controller that sets up the page autofill. I need the isbn text_field value, not its label value which is what I think I am obtaining. How do i grab this value? Thank you for any help.
You can use Javascript to do that as outlined in this SO post.
If you want to do it with rails, you can create a new action in the book controller and link to it in the form instead of autofill_books_path, for instance get_isbn:
def get_isbn
#isbn = params[:book][:isbn]
redirect_to autofill_books_path(isbnvalue: #isbn)
end
Of course you should include validation to check if the isbn provided is correct.

link_to_unless_current assign class to disabled (current) link

Anyone know if you can assign a tag and class to the disabled or current link? The example below only displays as plain text in the browser for the current link.
I have a bit of rails code displaying a list of buttons for each design in the database.
<% #id_cards.each do |id| %>
<%= link_to_unless_current id.design_type, id_card_design_path(id.id), :class => 'btn' %>
<% end %>
The active links are assigned the correct class and display as buttons.
link_to_unless_current accepts a block which can be used to override the default behavior.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_unless_current
<%=
link_to_unless_current("Comment", { :controller => "comments", :action => "new" }) do
link_to("Go back", { :controller => "posts", :action => "index" })
end
%>
In the example above it would yield the 'Go back' link if the current page was the 'new comment' page.
#James gave proper answer its just you are too young to take it right :)
<% #id_cards.each do |id| %>
<%=
link_to_unless_current(id.design_type, id_card_design_path(id.id), :class => 'btn') do
content_tag(:p, id.design_type, :class => :some_class
end
%>
<% end %>

Remove default 'GET' method parameters from URL

I'm using the generic search form, and my url after the search looks like
http://localhost:3000/search?commit=Search&page=2&query=feature&utf8=%E2%9C%93
The search works fine, but I would like to remove the default "utf8=✓" and "commit=Search" parameters from the URL, I'm also using will_paginate and I would like the &page=2 to be after the query parameter leaving it like this:
http://localhost:3000/search?query=feature&page=2
My code:
#posts_controller.rb
def search
query = '%'+params[:query]+'%'
#posts = Post.find(:all, :conditions => ["content LIKE ? or title LIKE ?", query, query]).paginate(:page => params[:page], :per_page => 5)
end
and
#html form
<%= form_tag(search_path, :method => 'get') do %>
<%= text_field_tag "query" %>
<%= submit_tag "Search" %>
<% end %>
and
#routes.rb
match '/search', :to => 'posts#search'
Thanks.
See similar questions:
Rails 3 UTF-8 query string showing up in URL?
removing "utf8=✓" from rails 3 form submissions
Basically, to remove 'commit=Search' add :name => nil to the submit_tag. IE needs the utf8 character. However, the second link has a initializer method to remove that part.
In this video, Ryan Bates talks about the name: nil fix (without ajax): http://railscasts.com/episodes/37-simple-search-form
You cant just remove it from url as far as YOU send it.
To clean up will_paginate try this
<%= will_paginate #whatever, params => params.merge({:commit => nil, :utf8 => nil}) %>
I solved utf problem by using
<form action="<%= root_path %>" method="get" >
...
</form>
instead of form_tag, it solved it.
Ryan Bates did a nice screen cast on exactly what you're trying to do (plus some more).
http://railscasts.com/episodes/240-search-sort-paginate-with-ajax

Rails content_tag inserts extra "<" and ">" characters

When doing this:
def user_log
if logged_in? == false
form_tag session_path, :id => "mform" do
content_tag(:span, content_tag(text_field_tag :email, "email#domain.com"), :class => "memail")+
content_tag(:span, content_tag(password_field_tag :password, "12345678912"), :class => "mpass")+
content_tag(:span, content_tag(submit_tag 'Login'), :class => "mbutton")
end
else
...
end
end
end
I get this:
stack overflow doesn't let me post pictures
Since I don't want the extra "<" and ">", what am I doing wrong?
EDIT: As extra information, on my view I am just doing:
<%= user_log %>
The fundamental problem is that you are using content_tag twice when you don't need to. content_tag essentially calls content_tag_string. Here's content_tag_string's source:
def content_tag_string(name, content, options, escape = true)
tag_options = tag_options(options, escape) if options
"<#{name}#{tag_options}>#{content}</#{name}>".html_safe
end
Calling content_tag(text_field_tag :email, "email#domain.com") looks like:
"<#{text_field_tag :email, "email#domain.com"}>"
and text_field_tag already produces a full HTML tag (it includes the "<" and ">").
All you need to do to get rid of the extra angled brackets is to leave out the second content_tag:
content_tag(:span, text_field_tag(:email, "email#domain.com"), :class => "memail")+
Though I haven't tried it locally, the problem is likely that Rails is html escaping your handy helper method. To see if I'm right, try throwing this in your view:
<%= raw(user_log) %>
If that works, you can throw raw in your helper method instead:
def user_log
if logged_in? == false
raw(form_tag session_path, :id => "mform" do
content_tag(:span, content_tag(text_field_tag :email, "email#domain.com"), :class => "memail")+
content_tag(:span, content_tag(password_field_tag :password, "12345678912"), :class => "mpass")+
content_tag(:span, content_tag(submit_tag 'Login'), :class => "mbutton")
end)
else
...
end
end
raw tells Rails that this code is safe and doesn't need to be html escaped.
As a first guess, I might try something like:
if logged_in? == false
...
else
...
end.html_safe
Update: Ok, back to the drawing board.
As a second guess, try this. (And note the extra parameter to content_tag, which required putting the hash in explicit { }...)
def user_log
if logged_in? == false
form_tag session_path, :id => "mform" do
(
content_tag(:span, content_tag(text_field_tag :email, "email#domain.com"), {:class => "memail"}, false)+
content_tag(:span, content_tag(password_field_tag :password, "12345678912"), {:class => "mpass"}, false)+
content_tag(:span, content_tag(submit_tag 'Login'), {:class => "mbutton"}, false)
).html_safe
end
else
...
end
end
end