Assume you have this:
<%= form_for post, remote: true do |f| %>
<!-- some other fields -->
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit %>
<% end %>
I easily solve the problem while displaying content using post.content.gsub(/\n/, "<br />"), but while I am editing the post's content, the textarea completely ignores the newlines (since there is no way to run gsub on f.text_area :content).
Due to this, long texts with several paragraphs completely lose their newlines.
I found many such questions like this, but never specifically for this scenario.
OK, so the solution was quite simple. Thanks to apneadiving for bringing to my attention the fact that I can simply add value: post.content to manually populate the content of the post to the textarea field, which allows me to manipulate it.
This solved my problem:
<%= f.text_area :content, value: post.content.gsub(/\n/, '\n') %>
Related
I have a form in Rails which uses fields_for to accept nested attributes:
<%= form_with(model: #combat_tracker, url: form_url) do |f| %>
…
<%= f.fields_for :zones do |zone| %>
<div class="zone-field">
<%= zone.text_field :name %>
<%= zone.check_box :_destroy %>
<%= zone.label :_destroy, "Remove zone" %>
</div>
<% end %>
…
<% end %>
Currently this gives me input fields for any existing zones on #combat_tracker. I want to add a button that will dynamically add a new zone-fields div for a new zone to be added when the form is submitted.
I’m using Rails 7 and assume the solution will involve the use of Turbo or possibly Stimulus, but can’t quite figure out the best way to do this. Thanks.
I don't think you need Turbo or Stimulus. Take a look at cocoon gem, it should do exactly what you're looking for.
Explaining all process here is quite complex for me, but try to follow this guide if the gem's one is too long.
I have a ruby code.
<%= f.label "Email *" %>
And I want to translate the English word "Email" to Japanese word because I am currently internationalizing my website written in English.
I tried the following.
<%= f.label "<%= t(:email) %> *" %>
However, it did not work. What should i do?
You should not use <% %> within another <% %>. Write it like this:
<%= f.label "#{t(:email)} *" %>
The <% tag already switches to ruby, where = also will render the outcome of the code. In order to get a translation, you might want to use <%= f.label t("email") %> and use email in your translation files.
As you need a translation inside a form, if it is bound to a model you can also translate your model as described inside the translate your models section inside the guides. Then <%= f.label :email %> will translate out of the box.
There are several ways to mark required fields, you may simply do <%= f.label "#{t("email")} *" %> or style the required field via css, like:
# app/view/.../....rb
<%= f.label :email, class: 'required' %>
# app/assets/stylesheets/.../....css
label.required:after {
content: " *";
}
I am useing a form_for helper to collect data on the client side of my application. However something weird is happening. I am not collecting the :name and :description and they are both returning as nil. this is my code:
<%= form_for #type do |f| %>
....
<%= f.text_field :name, :class => "col-xs-4" %>
<%= f.text_field :description, :class => "col-xs-4" %>
<%= f.submit %>
....
Do I need to make a fields_for under the form_for to get this working? It is a bit tricky because I am using #type which in this case is set up to tell the view which kind of attr. they are looking at. For example, this line:
<%= f.label #type %> <label> Description</label>
depending on what view you are on shows ether:
Group Description
or
Tag Description
and because they are both technically the same, I am using the same index for both. I hope I am clear with my issue and thank anyone who understands my problem and solution.
The param name will depend on the object you pass.
If #type contains an instance of Group, then you will get the params under params[:group], and if it is an instance of Tag, the you will get them on params[:tag]
<%= form_for #type do |f| %>
<%= f.label :name, "#{#type.model_name} Description" %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
Note the label definition. The way you are defining it will create 2 labels and the second one will not be linked to any field.
fields_for is normally used when you are creating several objects within the same form, for instance a Project and several tasks associated to it.
Hope this helps.
update:
If #type is a string or symbol it should work too. The tradeoffs using this approach will be that if there are any validation errors when creating the object, those will not be displayed and the fields will not be prefilled with the input that the user gave before submitting the form, forcing the user to enter all the information again and guessing which was the validation error (you can initialize it from the received params, but that complicates the code readability)
The unique thing different in your view would be the label definition.
<%= f.label :name, "#{#type} Description" %>
In rails is there any simple way to implement select or create from view.
Eg:
Product has_many(or has_one) Tags.
While creating new Product I can select existing tags or create new one.
This can be done by using JavaScript and other ways are there.. But all will take more time and effort.
Please share if you know other simple way...
Edit:
Something like this.
But imagine you have 100 tags or more ! your page will look bad with 100 checkbox or more..., one elegant way to do this is by using a jQuery plugin called jQuery Tokeninput i use it in my project and it's very helpful for what do you want, you can find the plugin Here
This is a screencast on how to use it : Token fields
and this is the revised version : Token Fields (revised)
check also this blog post about the same plugin if you want too How to create a token input field where the user can also add new items
cheer
Yep.
You are after nested forms. Try, https://github.com/ryanb/nested_form
For example,
<% form_for #product do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<% f.fields_for :tags do |tag| %>
<p>
<%= tag.label :content, "Tag Name" %><br />
<%= tag.check_box :content %>
</p>
<%= tag.link_to_remove "Remove this tag" %>
<% end %>
<%= tag.link_to_add "Add new tag" %>
<p><%= f.submit "Submit" %></p>
<% end %>
Setup the controller and model as given in the documentation and try the above code in the view. This railscast will help you a lot in figuring nested forms http://railscasts.com/episodes/197-nested-model-form-part-2
I'm using the Refinery content management system with a the Portfolio plugin
see http://github.com/resolve/refinerycms
I wanted to create a title and description for images uploaded to
Refinery using Refinerycms-Portfolio
so far I have done the following;
added the columns to the images table;
$script/generate migration AddTitleToImages title:string
$script/generate migration AddBodyToImages body:text
$rake db:migrate
modified the field div in this file, * highlighted
vendor/plugins/images/app/views/admin/images/_form.html.erb
<div class='field'>
<%= f.label :uploaded_data, 'Image' %>
<% if params[:action] =~ /(edit)|(update)/ %>
Use current image
<em>or</em>, replace it with this one...
<% end %>
<%= f.file_field :uploaded_data %>
***** <%= f.label :title, 'Title' %>
***** <%= f.text_field :title %>
***** <%= f.label :body, 'Description' %>
***** <%= f.text_area :body, :class => "wymeditor", :rows => 7 %>
</div>
Added these lines to the main image partial in
vendor/plugins/refinerycms-portolio/app/views/portfolio/
_main_image.html.erb
<h2><%= #image.title %></h2>
<p><%= #image.body %><p>
This works in the back-end except for a few visual bugs.
The problem with this is when i click through the thumbnails in the
front-end the titles and descriptions keep stacking on top of the
previous titles and descriptions. The main image changes fine, but
instead of refreshing the title and description, it adds the new one
above the previous titles and descriptions.
How can i stop this repetition so that only one title and description
will show at a time?
I'm new to Rails and I am using Rails-2.3.5 and I suspect this can be solved using Java Script
any help will be greatly appreciated,
John
You will need to edit the portfolio.js file in the /public/javascripts directory to show/hide the correct titles and descriptions on the portfolio page. This file handles all the AJAX features for the front end of the portfolio plugin.
Best of luck!