Radio button data not saved - ruby-on-rails

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.

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

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.

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

Passing User_ID through new_item form

I have a simple app that allows users to create 'items'. On the _form, the only data that it asks for is 'content' and 'user_id', which is currently a number picker that assigns user_id to the item for ownership. But what I want to do is have the form assume that the user_id is the current user's ID (using Devise). That way other people can't assign 'items' to other users. Make sense? Here's the form.
_form.html.erb
<%= 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You should be working with associations from the model.
example your Items model should have belongs_to :user
then your would just use the :user method not the user_id attribute.
But you really could make this much more simpler.
install simple_forms gem it will make your life easier.
gem "simple_form"
then
<%= simple_form_for(#item) do |f| %>
<%= f.input :content %>
<%= f.association :user %>
<%= f.button :submit %>
<% end %>

Rails 3, mysql join tables and habtm

I created 2 tables called products and brands and created a join table called brands_products via migration.
In each of the models I wrote the corresponding has_and_belongs_to_many setting.
In a form if have the following code:
<%= form_for(#product) do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :brand %><br />
<%= f.text_field (what to write?) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I don't know how to add a brand to a product intuitively, like the way Rails usually works...any thoughts?
You may want to look at accepts_nested_attributes_for. You'll also want to look at fields_for. Without seeing more of your data model, it's hard to give a detailed answer.
Generally, it would look something like:
<%- f.fields_for :brands do |m| -%>
<%= m.text_field :name %>
<%- end -%>

Resources