I have a problem with datetime attribute in Rails model.
model:
attr_accessor :from, :to, :via, :datetime
erb template:
<%= f.text_field :from %>
<%= f.text_field :to %>
<%= f.text_field :via %>
<%= f.datetime_select :datetime, :discard_year => true %>
I got:
#datetime(4i)' is not allowed as an instance variable name
On this line: #search_form = SearchForm.new params[:search_form]
What's wrong?
I don't know exactly what's causing the error, but I can tell you that to fix it, you'll need to change the attribute name datetime to, say sent_on (most anything else, really).
Why the error? I'd guess it's because datetime is a data type in SQL, and thus shouldn't be used as a column name, but the error seems to be coming from ruby - either your model or ActionView, so I'm not sure if that supports my theory...
Anyway, hope this helps!
Update: I just tested it, and you can use "datetime" as a column name in SQL (MySQL, at least). There goes that theory - it's a Rails thing, then, I guess...
Related
In my rails app, I have two models, a ClientPage and a ContentSection, where ClientPage has_many :content_sections. I'm using the nested_form gem to both models to be edited with the same form. This works fine as long as the ClientPage has at least one ContentSection, but if there are no associated ClientSections, the using nested_form's link_to_add method throws the following NoMethodError:
undefined method `values_at' for nil:NilClass
The form is structured as follows:
<%= nested_form_for page, form_options do |f| %>
# ClientPage fields
# ClientSections
<%= f.link_to_add "Add new section", :content_sections %>
<% end %>
As long as there is at least one ClientSection associated with the page, this works fine. As soon as there isn't, the error is thrown. Removing the link_to_add also stops the error from being thrown. (There's actually a second nested model under ContentSection, and the same issue arises if there are no associated models.)
Not sure what I'm fairly obvious thing I'm missing, but any pointers or suggestions would be greatly appreciated.
Finally worked this out -- the error was due to the fact that I was using the gem in a slightly non-standard way. Within the form, instead of rendering all of the content sections the standard way:
<%= f.fields_for :content_sections do |section_form| %>
# section fields
<% end %>
I put it inside a loop, as I needed the index of each item (which is not stored within the model itself):
<% page.content_sections.each_with_index do |section, index| %>
<%= f.fields_for :content_sections, section do |section_form| %>
# section fields
<% end %>
<% end %>
The issue doing it this way is that the fields_for method does not get called if the association is empty, and as such the gem cannot build the blueprint for the object (which is used to add in the extra item when link_to_add is called).
The solution was to make sure fields_for got called even if the association was empty:
<% if page.content_sections.empty? %>
<%= f.fields_for :content_sections do |section_form| %>
# section fields
<% end %>
<% end %>
I have a model Product which has a belongs_to association with another model Type. In the product's form, I'm using formtastic to display a select tag with all the types available in the database, like this:
<%= f.input :type %>
The select is showing up OK, but each option of it is an object instance of the Type model as a string, for example:
#<Type:0x00eff180c85c8>
Instead of that, I'd like to display the 'title' attribute of it, like:
Electronic
Domestic
...
Any ideas?
Try the member_label option, it sounds like what you want to do:
<%= f.input :type, :member_label => :title %>
The documentation has more examples.
Simply add this in your model
def name
return self.title
end
My question is similar to this SO problem, but it does not answer my question:
Rails 3 default datetime format without UTC
I understand that I can add
Time::DATE_FORMATS[:default] = "%Y/%m/%d"
to my environment.rb and it will change my default time format accordingly.
So when I do Time.now.to_s I get the correct format.
However, my question is, how come this does not work with a form_builder. For example:
<%= f.text_field :date %>
will return a full UTC timestamp: 2013-01-25 07:45:21
I am aware that I can do this
<%= f.text_field :date, :value => #post.date.to_s %>
And it will give me the correct format.
But this solution seems hacky to me. Is this really the only way to do it?
I am not 100% sure. But the answer is most likely no. The rails formhelper just gives you whatever value that is in your database.
Here is where the value is retrieved for your reference:
http://api.rubyonrails.org/classes/ActionView/Helpers/InstanceTag.html#method-i-to_input_field_tag
There is no special processing of it, such as localize or to_s, which is where formatting is applied.
If the date looks like a timestamp (2013-01-25 07:45:21 UTC),
You could do something like the following to views you localize timestamps with:
<%= localize(#post.date, :format => :long) %>
According to your problem, you could try using date_select rather than text_field.
<%= f.date_select :date, :order => [:day, :month, :year] %>
I want to add new field to registration form.
For this:
1. I created field named user_name in my database
2. I changed my model
attr_accessible :email, :password, :password_confirmation, :remember_me, :user_name
3. I changed the view
<%= f.label :user_name %>
<%= f.text_field :user_name %>
But I got the error: undefined method 'user_name' for #User:0x1ff0e30
Could anyone help me?
How did you create the field? It looks like the model isn't recognizing that the field is there which could be one of two things.
First, you've not run the migration to add the field to the table in the database. This is the most common mistake of people when this error is encountered.
Second, you did add the field, but you added it to the wrong database. Less common, but still a potential possibility.
I'm new to Rails (and fairly new to programming in general) and I am building a web app for myself as a way to learn. Right now I am modifying scaffolded forms and such.
My question is with the "create" method in one of my controllers. There are two entities I am concerned with: the User table and the Habit table. I created a dropdown box in the _form partial for the Habit views to allow a person to select a user from a list of all available when creating a habit as below
<%= collection_select :user, :id, #users, :id, :first_name %>
The habit controller, of course, has
def new
#users = User.all
...
end
This works fine, and when the form submits it posts two hashes of parameters :habit and :user. Now, when I want to process the form input in the create method, I'm not sure how to use the syntax correctly and assign the user_id to the newly create habit. What I WANT to do is something like this
def create
#habit = Habit.new(params[:habit], params[:user])
end
This, of course, is improper syntax.
def create
#habit = Habit.new(params[:habit])
end
assigns the params from the :habit hash correctly, but then the user_id is left unset.
What works is the following, but the code is very lengthy, assigning each value manually.
def create
#habit = Habit.new(:user_id => params[:user][:id],
:description => params[:habit][:description],
:habit_method => params[:habit][:habit_method],
:time_reqd => params[:habit][:time_reqd],
:will_reqd => params[:habit][:will_reqd],
:active => params[:habit][:active])
end
So my question is, when dealing with a form that posts data in multiple hashes, what is the proper way to pass those parameters into some method in a controller?
So my question is, when dealing with a form that posts data in multiple hashes, what is the proper way to pass those parameters into some method in a controller?
Instead of saying Habit.new( <lots of stuff> ), just use Habit.new(params[:habit]). Rails will try to assign each key in the hash (in this case, the params[:habit] hash's keys) to a matching value on the object.
Thus, if params[:habit] has a :description key, it will be assigned to a field called description on your model. This is called mass assignment and is quite handy.
Now you can just do:
#habit = Habit.new(params[:habit])
#habit.user_id = params[:user][:id]
You may want to read the RoR Getting Started Guide, like this section, for more similarly handy features of Rails.
Change
<%= collection_select :user, :id, #users, :id, :first_name %>
To
<%= collection_select :habit, :user_id, #users, :id, :first_name %>
The existing scaffold code should just work after that
Alternate
<%= f.select :user_id, #users, :id, :first_name %>