Create several records at once in Rails - ruby-on-rails

In my app users can create a set of codes to used in marketing activities. Therefore I want a form in which a user can type the amount of codes he want to create and then the system should generate that many codes and save them to the database.
code.rb
class Code < ActiveRecord::Base
belongs_to :form
attr_accessor :amount
end
_form.html.erb
<%= form_for [:customer, #code] do |f| %>
<% if #code.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#code.errors.count, "error") %> prohibited this code from being saved:</h2>
<ul>
<% #code.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :amount %><br>
<%= f.text_field :amount %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
What I want to do now is to tell the controller:
take :amount
create as many SecureRandom.hex(3) as :amount specifies
save all codes to database (column for the codes in the table is named "code")
However, I totally don't know how to achieve this...

Related

Nested forms rails field does not update

I am trying to do nested forms like mentioned here.
http://guides.rubyonrails.org/form_helpers.html#nested-forms
The goal is as follows: I have multiple colli with one checkbox which can be checked. The colli list can be deleted or modified but the checks and their information need to stay.
Model
class Colli < ActiveRecord::Base
has_one :check, foreign_key: "subcontainerid", primary_key: "colliid"
accepts_nested_attributes_for :check, allow_destroy: true
end
class Check < ActiveRecord::Base
belongs_to :colli
end
So every colli has one check. The colliid from the colli table created a link with the check table using the subcontainer id.
Controller
Within the colli controller I whitelist the check_attributes.
def colli_params
params.require(:colli).permit(:colliid, :collinaam, check_attributes: [:id, :checked])
end
Form
My form looks like this.
<%= form_for(#colli) do |f| %>
<% if #colli.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#colli.errors.count, "error") %> prohibited this colli from being saved:</h2>
<ul>
<% #colli.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :checks do |checks_f| %>
<p>check start</p>
<div class="field">
<%= checks_f.label :checked %><br>
<%= checks_f.check_box :checked %>
</div>
<% end %>
<div class="field">
<%= f.label :colliid %><br>
<%= f.text_field :colliid %>
</div>
<div class="field">
<%= f.label :collinaam %><br>
<%= f.text_field :collinaam %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
If I do form_for :check I can't see the checkboxes. When I do form_for :checks I see a checkbox but it does not work. When clicking submit I see following error:
undefined method `checked' for nil:NilClass
<p>
<strong>Checked:</strong>
<%= #colli.check.checked %>
</p><p>
<strong>Collinaam:</strong>
<%= #colli.collinaam %>
Which means it did not get saved. Does anybody know how to fix this?
Try adding this to your form-
<%= f.fields_for :checks, #colli.check.build do |checks_f| %>
<p>check start</p>
<div class="field">
<%= checks_f.label :checked %><br>
<%= checks_f.check_box :checked %>
</div>
<% 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.

Rails select NoMethodError in Messages#new

I'm trying to put on a select box the Users who will receive the message.
What we have is one table named Messages, one named Users and one named Friendships, on message table we have user_id (sender) and friend_id (receiver) both foreign key to Users ID.
And this is the error returning: undefined method `friend_id' for #Message:0x56f6238
That been said, I'm trying to vinculate all Friendships entries with current_user id and then using friend_id to search on Users table all users that current_user is able to send message.
Any help with this error? I'm really new on Ruby On Rails development.
If I'm missing some info, let me know.
Thanks in advance
On our application, the new_message_path renders one partial which contains the following:
<%= form_for(#message) do |f| %>
<% if #message.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#message.errors.count, "error") %> prohibited this message from being saved:</h2>
<ul>
<% #message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Receiver" %><br />
<%= f.select :friend_id, Message.all.collect {|friendships| [ friendships.friend_id ] }, { include_blank: true } %>
</div><br>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And on our model named Message we have this:
class Message < ActiveRecord::Base
attr_accessible :content, :friend_id
validates_presence_of :content, :friend_id, presence => true
belongs_to :user
end

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