ActiveModel::MassAssignmentSecurity Error - concerning time_select in formtastic? - ruby

I'm using the following code to create some timeslect and date select objects using activeadmin and formtastic. The result generates two timeselects and a date select object as the correspoinding data field types are time and date. The problem is that when I submit the form I get a ActiveModel::MassAssignmentSecurity error
<%= f.inputs :class=>'inputs', :for=>Schedule.new do |fu| %>
<%= fu.input :eta, :label=>"ETA", :ignore_date=>true %>
<%= fu.input :etd, :label=>"ETD", :ignore_date=>true %>
<%= fu.input :date, :ignore_time=>true %>
<% end %>
When posting the etd and eta fields are posted as arrays - is there a way to get the actual values of them cleanly?

I can't know for sure what your problem is without seeing the models in question, but my best guess is that you've got an issue with accepts_nested_attributes_for and attr_accessible.
On the model represented by the parent form, you will need:
has_one :schedule #assuming it's has_one, could be something else...
accepts_nested_attributes_for :schedule
attr_accessible :schedule_details
Note that the association name in attr_accessible is appended with _details. If you just have the association name, Rails will throw an exception.
Does this look like it could be the issue?

Related

Phoenix Framework: Is inputs_for available for the has_many associations?

Using the Phoenix Framework, I want to implement the nested form like the fields_for of the RoR.
Today I tried to implement it with inputs_for, but I got the error.
Referring the official document of Phoenix Framework, I could found the sample with inputs_for. It describes only the sample with the embed_one association. And I met the error with the has_many association.
How can I implement the nested form for the has_many association correctly?
The schemata are as below:
defmodule AnApp.User do
use PhoenixBlog.Web, :model
schema "users" do
field :handle, :string
field :password_digest, :string
has_many :emails
end
end
defmodule AnApp.Email do
use PhoenixBlog.Web, :model
schema "emails" do
field :address, :string
end
end
And I implemented the form.html.eex as below:
<%= form_for #changeset, #action, fn f -> %>
<%= inputs_for f, :emails, fn ef -> %>
<% end %>
<% end %>
This cause the error Argument Error: unknown field :emails.
No, right now it supports only embeds. We are working on supporting associations next. The error messages in the newly released Phoenix.Ecto should also be clearer.

Can't see why I'm getting undefined method form_for

I don't get why I'm getting this error
undefined method `sector_id' for #<Portfolio:0x007fe17c2e3848>
I have a Portfolio Model and a Sector model, they look like so
class Portfolio < ActiveRecord::Base
belongs_to :sector
attr_accessible :overview, :title, :sector_id
end
class Sector < ActiveRecord::Base
has_many :portfolios
attr_accessible :name
end
My routes
resources :portfolios do
resources :sectors
end
So within my form to create a new portfolio I have this collection_select
<%= f.label :sector_id, "Choose Sector", :class => 'title_label' %><br>
<%= f.collection_select(:sector_id, Sector.all, :id, :name, :prompt => "Please Select a Sector") %>
This is something I've done many times before and it has worked, can anyone see why I would be getting this error?
The only thing I can think of is that I have called my controller for portfolio as Portfolios, I always get mixed up with plural and singular controller names, would this make a difference in my case?
Maybe you have not run the migration yet that adds the column "sector_id" in your table "portfolios". If you are using MySQL connect to your database and check the table (show create table portfolios;). Use appropriate method to get this info from your database server if you are using other rdbms. Alternatively, in your rails console (rails c) type in Portofolio and see what attributes it prints out. Does it include sector_id?

Conditional HTML attribute

In ActionView I need to display an attribute based on a condition.
<%= f.text_field :regmax_remote, {
:class => 'span2',
:style => "display:#{#event.regmax_remote.present? ? "block" : "none"};"
}
%>
Is there a prettier way to go about this?
The above code is fine, If you are going to use it only once in the,
But If this will be used in many places then u may need helper
def event_display_style event
event.regmax_remote.present? ? "block" : "none"
end
if you have multiple attributes based on several conditions then u can use the helper to return the attributes in hash format and use it like this.
<%= f.text_field :regmax_remote, event_display_style(#event) %>
if u want a variable hash with default hash then u can do something like this as well
<%= f.text_field :regmax_remote, {class: "span2"}.merge(event_display_style(#event)) %>
There are some other ways to make this code look better. U may also like the draper gem. which gives an object oriented control over displaying at the same time can acce view helpers.
https://github.com/drapergem/draper
You can try like the following,
<% if (#event.regmax_remote.present?) %>
<%= f.text_field :regmax_remote, class: "span2" %>
<% end %>
Do not copy the same, just edit as per your code and use this as the example.

Rails: has_one association error?

community, I need your help. It's a quite simple problem, but I can't figure out what's wrong.
I have two models, a Product model and a Condition model. A Product can have only one condition, so I set a one-to-many association between the two. Condition contains fixed records (e.g. good, bad, damaged)
Product.rb
attr_accessible :condition_id
has_one :condition
Condition.rb
belongs_to :product
I have a foreign key condition_id in my products table.
In my products form, I loop through the conditions and set the id:
_form.html.erb(Product)
<%= f.select :condition_id, Condition.all.collect {|x| [x.name, x.id]}, {} %>
I can see that the id is set in the console, when I submit the form. But I can't retrieve the value of the given condition.
In my product show page, I try
<%= #product.condition.name %>
but it gives me a "undefined method `name' for nil:NilClass" error. This is also happening when trying in the console.
What am I missing here?
Thank you all!
Sorry, for answering my own question.
It seems that I mixed-matched the association.
So a Product belongs_to :condition and a Condition has_many :products works fine.
However, for me Product has_one :condition and Condition belongs_to :product sounds much more verbose.

Question about associating models

I'm working on a Dungeons and Dragons character database. I have two models, character and statistic. I want this to work where each character has one set of statistics. The problem is, when I create a new character, every character shares the same statistic information. This is probably a really easy problem to solve, but I've been butting my head up against it and can't figure it out.
Here's code from the character model:
class Character < ActiveRecord::Base
has_many :statistics, :dependent => :destroy
end
Here's code from the statistic model:
class Statistic < ActiveRecord::Base
belongs_to :character
end
What's the proper code for displaying the statistic model when viewing the character? Do I need to use a link_to or a render tag? Thanks!
Since there is many of them, you'll need to use a loop or something:
<ul>
<% #character.statistics.each do |stat| %>
<li><%= stat %></li>
<% end %>
</ul>
Even better would be to use a partial:
<ul>
<% #character.statistics.each do |stat| %>
<%= render :partial => "characters/statistic", :object => stat %>
<% end %>
</ul>
Then you'll have app/views/characters/_statistic.html.erb:
<li><%= statistic %></li>
This way you can use the code for the statistic rendering in other places too.

Resources