ruby on rails: empty database records - ruby-on-rails-4

I am new to Ruby on Rails. I am building a basic application for learning purpose.
I build a form and when I use the Save button, a record is created but it is a empty record with only an Id.
I hope someone can help me.
routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :relaties
root 'welcome#index'
new.html.erb
<h1>Nieuwe Relatie</h1>
<%= form_for :relatie, url: "/relaties" do |f| %>
<p>
<%= f.label :naam %><br>
<%= f.text_field :naam %>
</p>
<p>
<%= f.label :straatnaam %><br>
<%= f.text_field :straatnaam %>
</p>
<p>
<%= f.label :huisnummer %><br>
<%= f.text_field :huisnunmmer %>
</p>
<p>
<%= f.label :postcode %><br>
<%= f.text_field :postcode %>
</p>
<p>
<%= f.label :plaats %><br>
<%= f.text_field :plaats %>
</p>
<p>
<%= f.label :omschrijving %><br>
<%= f.text_area :omschrijving %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
relaties_controller.rb
class RelatiesController < ApplicationController
def new
#relatie = Relatie.new
end
def create
#relatie = Relatie.new(params[:relatie])
#relatie.save
redirect_to(#relatie)
end
private
def relatie_params
params.require(:relatie).permit(:naam, :straatnaam, :huisnummer, :postcode, :plaats, :omschrijving)
end
def show
#relatie = Relatie.find(params[:id])
end
end

While creating/ updating a record you should whitelist the attributes that you would like to be saved in database. You have added a method named relatie_params to do that BUT you have not used it in the code which is why when you try to create a new Relatie record none of the attributes are stored in database as they are not white-listed.
In create action, replace
#relatie = Relatie.new(params[:relatie])
with
#relatie = Relatie.new(relatie_params)
For your reference, read about Strong Parameters which was introduced in Rails 4

Related

Ruby on rails Tutorial Missing partial articles/_form

Hi I have just started using Ruby on rails and have been following the tutorial # http://guides.rubyonrails.org/getting_started.html.
The problem I have got, is with 5.12 Using partials to clean up duplication in views.
I am getting the error ActionView::MissingTemplate in Articles#new.
It is looking for _form.html.erb , which is in the directory /app/views/articles/. So not sure why it cannot find it.
articles controller for new
def new
#article = Article.new
end
articles view for new
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
articles view for _form.html.erb
<%= form_for #article do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
< % end %>
Any help would be appreciated, need to know how to solve before going on to next part.
Hi I have got it to work by renaming _form.html.erb to_form as the error is looking for /apps/views/_form. I don't know if this is the correct way to solve the problem, as its says in the tutorial to name the file _form.html.erb.
would this cause issues in future doing it this way?
Check the file name, maybe you copied it wrong
I changed the file name to '_form.erb' and it worked. Still puzzled why the full suffix of 'html.erb' didn't work.

param is missing or the value is empty:

Here is booking controller:
class BookingsController < ApplicationController
before_action :logged_in_user, only: [:create]
def new
#booking = Booking.new
end
def index
#bookings = Booking.all
end
def create
#booking = current_user.bookings.build(booking_params)
if #booking.save
flash[:success] = "Booking created!"
redirect_to root_url
else
render 'static_pages/home'
end
end
def show
#booking = Booking.find(params[:id])
end
def destroy
#booking = Booking.find(params[:id])
#booking.destroy
redirect_to bookings_path
end
private
def booking_params
params.require(:booking).permit(:date, :hour, :game)
end
end
When I post after the utf8 and authenticity token I don't understand why "#<\Booking:0x000001061928a8>\" instead of simply "booking" then the nested hash contains date, hour and game which I permit in the controller.
Here is the view:
<%= form_for(#booking) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :day %>
<%= date_field(#booking, :date) %>
<%= f.label :hour %>
<%= number_field(#booking, :hour, in: 8..19) %>
<%= f.label :game %>
<%= select(#booking, :game, [['Singles', 1], ['Doubles', 2]]) %>
<%= f.submit "Book", class: "btn btn-primary" %>
<% end %>
Any help much appreciated.
Ok it was simply that I wasn't using the the f variable of the form builder for the select boxes, originally I was but I'd had problems rendering the select boxes so I'd taken it out. Must have been due to a syntax error.
Here's the working form_for with a date picker and other select boxes:
<div class="field">
<%= form_for(#booking) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :day %>
<%= f.date_field :day %>
<%= f.label :hour %>
<%= f.number_field :hour, in: 8..19 %>
<%= f.label :game %>
<%= f.select :game, [['Singles', 1], ['Doubles', 2]] %>
<%= f.submit "Book", class: "btn btn-primary" %>
<% end %>
</div>

Missing partial in Rails 4 project

in my simple project I decide to use nested form and using for it cocoon gem. but after implementing it I get Missing partial error, although I have partial file with underline. this is my cocoon logic code:
_form.html.erb
... form_for starts...
<div class="row">
<div class="col-md-6">
<h3>Ingredients</h3>
<%= f.fields_for :ingredients do |ingredient| %>
<%= render 'ingredients', f: ingredient %>
<% end %>
<%= link_to_add_association "Add", f, :ingredients %>
</div>
</div>
...submit and cancel buttons...
this is my partial file _ingredients.html.erb
<div class="nested-fields">
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :count %>
<%= f.text_field :count %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= link_to_remove_association "Remove", f %>
</div>
it's working after rename the partial file to _ingredient_fields.html.erb. ingredient in single, and fields in plural
It's looking for a file named _ingredient_fields. I assume it's cocoon providing the link_to_add_association method and it needs more partials.

Ruby on Rails Guides | partial variable - cannot contain nil or be empty

I am following Ruby on Rails Guides and I have an error in rendering partial when I want to create a new article:
First argument in form cannot contain nil or be empty
Marked error is from partial _form.html.erb located in the same place as index:
<%= form_for #article do |f| %>
index.html.erb has link:
<%= link_to 'New article', new_article_path %>
controller:
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
form partial:
<%= form_for #article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %><br>
</p>
<% end %>
new.html.erb which renders partial:
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
your controller needs a new method
def new
#article = Article.new
end
that way there's an #article object that the form_for can reference.
When you follow a new link, the new action is executed and the form is displayed. It's only when the form is submitted that the create action is executed.

Rails: Render for nested forms

I have a nest form that is working great. The form is basically a customer with addresses.
I am trying to render a partial at both the customer and address level to indicate who created each record and who was the last person to update the record.
My view code is:
<%= form_for(#customer) do |f| %>
<%= render 'cust_fields', f: f %>
<%= render 'layouts/audit', audit: #customer %>
<strong>ADDRESSES:</strong>
<hr />
<%= f.fields_for :addresses do |a| %>
<%= render "address_fields", f: a %>
<%= render 'layouts/audit', audit: :addresses %>
<% end %>
<% end %>
The code in question is <%= render 'layouts/audit', audit: :addresses %>
This is throwing the the error:
undefined method `created_by' for :addresses:Symbol
I have tried to change :addresses to #customer.addresse but that doesn't work either. Why?
My partial code is:
<% created_user = User.find(audit.created_by) %>
<% updated_user = User.find(audit.updated_by) %>
<div class="row audit-info">
<small>
<div class="pull-left">
Created by: <%= created_user.name %>
</div>
<div class="pull-right">
Last updated by: <%= updated_user.name %>
</div>
</small>
</div>
Both the customers and addresses table have created_by and updated_by columns.
Any help would be much appreciated.
I was able to get this working by introducing the following code in my view:
<% for i in 1..#customer.addresses.size %>
<%= f.fields_for #customer.addresses[i-1] do |a| %>
<%= render "address_fields", f: a %>
<%= render 'layouts/audit', audit: #customer.addresses[i-1] %>
<% end %>
<% end %>
Actually, the above didn't work since I was double counting the children records.
What did work (after some more research) was the following:
<%= f.fields_for :addresses do |a| %>
<div class="deleterow">
<%= render "address_fields", f: a %>
<%= render 'layouts/audit', audit: #customer.addresses[a.index] %>
</div>
<% end %>
The key was using a.index, which can be seen on line 4.