TinyMCE and rails - ruby-on-rails

I am a rails n00b.
I want to use tinyMCE instead of a textarea that got created automatically via scaffolding.
so Im using tinyMCE hammer. the scaffolding automatically created this line
<%= f.text_area :descripcion %>
and I substituted it with
<%= tinymce(:descripcion) %>
When I load the view it looks just fine but when I try to submit the descripcion field of my model object is null.... help please

You need to keep the f. if you're in a form_for block:
<%= f.tinymce :descripcion %>

Related

How can I create a button which adds fields to a Rails form

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.

Is it possible to use a React Component for one field of a Rails form?

I have a working Rails form in which I'm trying to implement one field as a React Component (as an input with autosuggester). I have the React autosuggester working perfectly (so I don't want to use a library for it), and now I'm trying to figure out how to get the selection back in the form_for data. The form currently looks something like this:
<%= form_for (#user, url: :interests_path, method: :put) do |f| %>
<%= f.fields_for :user_interests do |builder| %>
<%= f.text_field :title %>
<%= react_component(...(#interests already passed in successfully) %>
<% end %>
<% end %>
The react component has an input field in it, and I'd like to get the value of that back into the form somehow. Is there a way to grab it from its parent container? Any suggestions greatly appreciated.

styling results from a text field in rails

I am building a simple Rails blog, using a text field to write posts like this:
<p>
<%= f.label :content %>
<%= f.text_field :content, :size=>"50" %>
</p>
How can I style/format the output from this? When i submit a post and look at the developer tools in Chrome, the text is not added to a paragraph element so I don't know how to style it..
After submitting the form, the rails default is to load the model's show.html.erb template. Edit that template to change it structurally or apply CSS to the elements therein.

Rails how to programatically get html id from form

Say I have a Rails form like the following
<% form_for #model do |f| %>
<%= f.label :column %>
<%= f.check_box :column %>
<% end %>
Is there a way I can get the html id that will be generated for the 'column' check box? What would be great is if there was a way to add in
<%= f.observe_field :column, options %>
Anyone know how to add this to FormBuilder?
For generating an element's id programatically, see this. As it seems, there is no easy way out.
You could add this to FormBuilder by wrapping up certain field tags. See for example this.

Best way to reload parts of a form

I want to reload a part of a form generated with the form_for-helper via AJAX.
After reloading the part I still want to have access to the form object.
How can I do this?
Best regards
I am not sure if your are using different terminology than I've heard, but what do you mean "still want to have access to the form object"?
Do you mean access to it in JavaScript? That should still work as long as you don't overwrite the form tags.
Do you mean in the html.erb code generating the partial? That doesn't really make sense, because that form_for object has already generated its html tags and gone out of scope. You need to use to the regular form of the helpers that takes the name of the object as the first parameter. There is no problem with this working with the tags generated by the form_for version of the helpers.
So, in your main page:
<%= form_for :person, #person, :url => { :action => "create" } do |f| %>
<%= f.text_field :name %>
<div id="reloadable">
</div>
<% end %>
And in your partial that fills that div:
<%= text_field :person, :name %>
No step 3.

Resources