Rails: tinyMCE won't load with my partial - ruby-on-rails

Once more, I'll need your help with Rails. This time, I've a problem with tinyMCE. I can get it working on my project but not when I'm trying to render a partial.
Ok, so what I'm trying to do is displaying a collection of textarea with tinyMCE. For that, I call a partial. Let me show you my code, it would be easier to understand.
So i have my template where I want to display everything:
#index.html.erb
<% if !#online_cv_fields.empty? then %>
<%= render :partial => 'online_cv_fields/form', :collection => #online_cv_fields %>
<% end %>
Then the partial i'm calling
<%= form_for(form) do |f| %>
<% if form.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(form.errors.count, "error") %> prohibited this online_cv_field from being saved:</h2>
<ul>
<% form.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :field_name %><br />
<%= f.text_field :field_name %>
</div>
<div class="field">
<%= f.label :field_content %><br />
<%= f.text_area :field_content, :class => "mceEditor" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And I'm loading my javascript in my application layout
<%= javascript_include_tiny_mce_if_used %>
<%= tiny_mce if using_tiny_mce? %>
My problem is that the Javascript won't load for the index. Even if I try to "force" the application to load tinyMCE with
<%= javascript_include_tiny_mce %>
<%= tiny_mce %>
It just doesn't work.
Any clue?
Thanks !

What mode are you using? Does TinyMCE know you want you want it to pick up the 'mceEditor' class? See: http://tinymce.moxiecode.com/wiki.php/Configuration:mode and http://tinymce.moxiecode.com/wiki.php/Configuration:elements
You might also like to try TinyMCE Hammer: http://tinymcehammer.lanalot.com/

Related

how to add method with gsub to text_field ?

i'm new to rails and ruby, sorry if it's too noobish to ask but need help cause i'm stuck.
i have simple 'app', using rails forms. I need to change ( . ) into ( : ) whenever user will fill the text_field on form.
can anyone be so kind and help me create method from scratch, to access text_field : appearance and use gsub method on it to change ( . ) into ( : ) ?
here is my view: (model is empty, controller is generated using scaffold, super simple stuff)
<%= form_for(#sample) do |f| %>
<% if #sample.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#sample.errors.count, "error") %> prohibited this sample from being saved:</h2>
<ul>
<% #sample.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :url %><br>
<%= f.text_field :url %>
</div>
<div class="field">
<%= f.label :appearance %><br>
<%= f.text_field :appearance %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In your model, try this:
class Sample < ActiveRecord::Base
def appearance=(value)
write_attribute(:appearance, value.gsub(/\./, ':'))
end
end

Radio button data not saved

I'm playing around with Form Helpers. I found some code from another SO question and thought it was pretty efficient at creating radio buttons with an elegant loop. Now that I've incorporated it, it doesn't save the data (e.g. the category value is not being saved to the project table)
Please see code below of _form.html.erb
<%= form_for(#project) do |f| %>
<% if #project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% #project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="form_row">
<label for="category">Category:</label>
<% [ 'checklist', 'process'].each do |category| %>
<br><%= radio_button_tag 'category', category, #category == category %>
<%= category.humanize %>
<% end %>
<br>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Your radio button parameter is being created outside of the project structure. If you look at params, you'll probably see
{:category => "your_category", :project => {...project params...}}
It is because you're using the radio_button_tag instead of the regular form helper. Try this instead:
f.radio_button :category, category, :checked => (#category == category)
Also, as Justin said, make sure :category is included in the project_params in your controller.

How do I use select_year with form_for?

I have a form as follows:
<%= form_for(#car, :html => {:class => 'form-horizontal' }) do |f| %>
<% if #car.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#car.errors.count, "error") %> prohibited this car from being saved</h2>
<ul>
<% #car.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :model_year %><br />
<% %>
<% end %>
</div>
I want to have a collection select for the model year starting 100 years back and going to a year in the future. I have tried
<%= f.select_year :model_year%>
but it says that select_year is not a valid method. Tried
<%= f.date :model_year, :discard_month => true %>
also to no avail. Any thoughts?
select_year is a helper that generates a full select tag with options. Since you're using form_for instead of form_tag, you'll want to use a helper that can be called on a form builder object.
<%= f.select :model_year, (Time.zone.now.year - 100)..(Time.zone.now.year + 1) %>
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_year

Rails 3 form rendered in ajax get html mixed with text_fields values

I'm rendering a form by ajax this way:
$('#details').html("<%== escape_javascript(render ('form')) %>");
Thanks to d11wtq !
Now the problem is that this form is renderend with some html as text! although all the values in the text-feild and text-area are correct.
This is the form
<%= form_for #note, :remote => true, :html => { :'data-type' => 'html', :id => 'new-note-form' } do |f| %>
<% if #note.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#note.errors.count, "error") %> prohibited this note from being saved:</h2>
<ul>
<% #note.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :wine_id, :value=>#note.wine.id %>
<%= #note.wine.name %>
<div class="field">
<%= f.label :rating %> <br />
<%= f.text_field :rating %> </div>
<div class="field">
<%= f.label :text %> <br />
<%= f.text_area :text,:size => "60x12" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
So when this form is rendered, after the block <%= #note.wine.name %> Comes Red Wine div> printed out. In the text-field comes the value and some html in it. And the same in the text-area. The html code inserted is always the some, so it's not random.
Thanks for your time and your help :)
$('#details').html("<%= raw escape_javascript(render ('form')) %>")
make sure the raw is passed because some newer versions of rails are having problems escaping java scripts and html at the moment. also make sure you selector is in place. And have a respond to js formart in your controller, and a js.erb template in your view.

Rails Screencast - Completely stuck on "fields_for"

i am following this simple tutorial and i followed all the steps... but the browser simply doesn't show me anything i put inside the fields_for tag.
<%= form_for :activity, :url => activities_path do |f| %>
<% if #activity.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#activity.errors.count, "error") %> prohibited this activity from being saved:</h2>
<ul>
<% #activity.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Foto" %><br />
<%= f.text_field :photo %>
</div>
<div class="field">
<%= f.label "Foto per la home" %><br />
<%= f.text_field :photoHome %>
</div>
<% for info in #activity.infos %>
This is rendered<br>
<% fields_for "activity[info_attributes][]", info do |info_form| %>
This is not rendered<br>
<p>
Titolo: <%= info_form.text_field :title %>
</p>
<p>
Descrizione: <%= info_form.text_field :description %>
</p>
<% end %>
<% end %>
<p><%= submit_tag "Create activity" %></p>
<% end %>
The above code result is this:
What am i missing?
Rails is sometimes (well ok, often) a bit confusing in which helpers want a <% vs which helpers want <%=. Try
<%= fields_for "activity[info_attributes][]", info do |info_form| %>
The tutorial you're following doesn't, but that's from 2007, and in Rails 3 this was changed. Current documentation on fields_for is here.

Resources