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.
Related
I successfully added a column "name" to "posts", When I check the post in the rails console it says name is nil even after I added a post to the form.html, this is the form file.
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post
from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :description %>
<%= form.text_field :description, id: :post_description %>
</div>
<div class="field">
<%= form.label :Name %>
<%= form.text_field :name, id: :post_name %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Make sure you have added that attribute in your permitted param list in your controller.
So I'm following a tutorial and near 1:07:39 in the New Review page, he has rating field and a comment field. But for me, only the comment page shows up, and I've tried copying his movie controller, reviews controller and the _form.html.erb. I am using ruby on rails and using the cloud 9 editor. All help is appreciated. If there's anything I have to add please comment.
This is my form:
<%= form_for([#movie, #review]) do |f| %>
<% if #review.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#review.errors.count, "error") %> prohibited this review from being saved:</h2>
<ul>
<% #review.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<div id="star-rating"></div>
</div>
<div class="field">
<%= f.label :comment %><br>
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<script>
$('#star-rating').raty({
path: '/assets/',
scoreName: 'review[rating]'
});
</script>
Your div for the rating is (probably) missing the label and text_field. You should do something like this:
<div class="field">
<div id="star-rating"></div>
<%= f.label :rating %><br>
<%= f.text_field :rating %>
</div>
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.
This is what Things look like on my site.
I want "Cole Haan Air Madison" to actually link to the Cool Haan website where users can purchase that item.
This is the code for the form used to create Things:
<%= simple_form_for(#thing, html: { multipart: true }) do |f| %>
<% if #thing.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#thing.errors.count, "error") %> prohibited this thing from being saved:</h2>
<ul>
<% #thing.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :title %><br>
<%= f.text_field :title, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :description %><br>
<%= f.text_field :description, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :image %><br>
<%= f.file_field :image, class: 'form-control' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The form visually looks like this:
I know I have to add a link field to the form. But how do I, instead of displaying the link to the user, have it correspond to the title of the Thing?
Here is the show view for Things:
<div class='row'>
<div class='col-md-offset-2 col-md-8'>
<div class='panel panel-default'>
<div class='panel-heading text-center'>
<%= image_tag #thing.image.url(:medium) %>
</div>
<div class='panel-body'>
<p>
<strong><%= #thing.title %></strong>
</p>
<p>
<%= #thing.description %>
</p>
<p>
<%= link_to #thing.user.username, #thing.user %>
</p>
<% if #thing.user == current_user %>
<%= link_to edit_thing_path(#thing) do %>
<span class='glyphicon glyphicon-edit'></span>
<% end %>
<% end %>
</div>
</div>
</div>
Basically, you need link_to tag.
Since, you mentioned:
I want "Cole Haan Air Madison" to actually link to the Cool Haan
website where users can purchase that item.
So, I consider that you are storing website of the user's thing.
Replace,
<strong><%= #thing.title %></strong>
in your code with:
<strong><%= link_to #thing.title, #thing.user.website %></strong>
Here, #thing.user denotes the associated user of that thing, and #thing.user.website gives back the website of that user.
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.