Rails Screencast - Completely stuck on "fields_for" - ruby-on-rails

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.

Related

Rails: syntax error, unexpected end, expecting ')' '.freeze; end ^~~

I'm creating a very basic rails app (learning tutorial) and can't understand why I'm getting this error.
I've tried troubleshooting but to no avail.
<div id="page_wrapper">
<h1> Make Something </h1>
<%= form_for :post, url: posts_path do |f| %>
<% if #post.errors.any? %>
<div id="errors">
<h2> <%= pluralize(#post.errors.count, "error" %> stopped this post from saving </h2>
<ul>
<% #post.errors.full_message.each do |msg| %>
<li> <%= msg %> </li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %> <br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %> <br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
I expect the better UI error messages, but no idea what my error is - I'm sure it's a very minor syntax fix, but your help would be much appreciated.
You're not closing the brackets when calling pluralize
change
<h2> <%= pluralize(#post.errors.count, "error" %> stopped this post from saving </h2>
to
<h2> <%= pluralize(#post.errors.count, "error") %> stopped this post from saving </h2>

Making a survey app, how should I delete questions (and add some layout)?

I'm making a survey app using a Survey model, which has_many Questions which itself has_many answers. The form partial looks like this:
<%= form_for(#survey) do |f| %>
<% if #survey.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
<ul>
<% #survey.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>
<%= f.fields_for :questions do |question_attribute| %>
<p>
<%= question_attribute.label :content, "Question" %><br />
<%= question_attribute.text_area :content, :rows => 3 %>
</p>
<%= question_attribute.fields_for :answers do |answer_attribute| %>
<p>
<%= answer_attribute.label :content, "Answer" %>
<%= answer_attribute.text_field :content %>
</p>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and is called by the edit and new actions. quick side note: how is it that rails fills in previous question/answer values in the form when called by the edit action?
I want to make it so that when I visit each survey's edit page, beside each question will be a "delete" link, and when I press that delete link the question is deleted from the survey and I'm directed back to the edit page. How should I accomplish this? I took a look at some railscasts but everything they did had been deprecated already.
In addition, right now when I show a survey, the questions aren't numbered ie Question 2, Question 2, etc. and the Answers for each question aren't numbered a), b), c), d), e). How do I make rails automatically number these when I create a survey? My show form looks like this:
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #survey.name %>
</p>
<ol>
<% #survey.questions.each do |question| %>
<li><%= question.content %>
<ul>
<% for answer in question.answers %>
<li><%= answer.content %></li>
<% end %>
</ul>
</li>
<% end %>
</ol>
<%= link_to 'Edit', edit_survey_path(#survey) %> |
<%= link_to 'Back', surveys_path %>

Try to generate a dynamic field in Rails

I want to generate a dinamic field in my form on Rails. This is the code:
<%= form_for(#item) do |f| %>
<% if #item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% #item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :vehicle %><br>
<%= f.text_field :vehicle %>
</div>
<div class="field">
<%= f.label :part %><br>
<%= f.text_field :part %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My idea is put a script into the form where i can generate a field part dinamicaly with a button with a text like "add" for another part field. Any idea?? Thanks everyone.

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.

Rails: tinyMCE won't load with my partial

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/

Resources