I've just added 2 new columns to my user table: photo and sex.
For some reason when I create a new user and fill up the entire form(name, photo and sex) it does not stay saved.
When I go to edit it just shows the name I've filled in, but the photo and sex fields are empty everytime.
Any idea what might be causing this?
Here's the form:
<div class="field">
<%= f.label :name, "Full Name" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :photo_1, "Photo" %><br />
<%= f.text_field :photo_1 %>
</div>
<div class="field">
<%= f.radio_button(:sex, "male") %>
<%= f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female") %>
<%= f.label(:sex, "Female") %>
</div>
<div class="actions">
<%= f.submit %>
</div>`
Firstly I would rename that column from sex to gender.
Secondly check the attr_accessible declarations on your model, you may need to add these fields to the list
Related
how to do dropdown select in rails form ?
i tried to
<div class="field">
<%= f.label :car_id %>
<%= select("car", "car_id", #cars) %>
</div>
<div class="field">
<%= f.label :firstname %>
<%= f.text_field :firstname %>
</div>
<div class="field">
<%= f.label :lastname %>
<%= f.text_field :lastname %>
</div>
<div class="field">
<%= f.label :dateofbirth %>
<%= f.date_select :dateofbirth %>
</div>
but i get this error
undefined method `empty?' for nil:NilClass
at this line.
<%= select("car", "car_id", #cars) %>
Try
<%= f.select :car_id, #cars.collect { |car| [car.name, car.id] } %>
You need to set the #cars instance variable in the controller method for the above line of code to work.
I am assuming that car has a name field. Replace name with a field of your Car model that is good enough for a label.
For every option in the selectlist, name will be set as the label and id will be set as the value.
For more, read the documentation here
I'm working on a advanced search form. I am working in views/searches. Now I have attributes created for user profiles that I have been using such as zip code, age, gender, career, religion, education, etc. I want to use these fields for my advanced search.
When I include the f.label and text fields I get a undefined method. I'm hoping I don't have to recreate each attribute for the search form, as that would not make much sense considering I have already done all these attributes for the user profile. Any help would be greatly appreciated!
/searches/new.html (for search):
<%= form_for #search do |f| %>
<div class="field">
<%= f.label :keywords %><br/>
<%= f.text_field :keywords%>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
/users/new.html (for the user profile):
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
</div>
<div class="field">I'm a
<%= f.select :gender, ['man', 'woman']%>
interested in
<%= f.select :gender, ['women', 'men', 'both']%>
</div>
<div class="field">
Age <%= select(#object, :age, (18..75).to_a) %> to <%= select(#object, :age, (18..75).to_a) %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
Your #search object is a Search object (or whatever it actually is), not a User.
Rails doesn't know your Search object doesn't actually have those fields, so when it tries to retrieve the fields that don't exist, it'll blow up.
There are any number of ways around this, including giving your Search a User property.
You could also create a new User and pass it to a user form partial as f, that's probably the approach I would take, although I don't know precisely what it would look like without trying it.
Newbie to rails and on many online tutorials I see this example
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
which stores like this
"first_name"=>"foo", "last_name"=>"bar"
Is there a way use text_field or another form attribute to store two inputs in an array, so it would end up something like:
"name"=>["foo", "bar"]
This is not the preferred way, but curious to see how it would be done
Update
In your example you are passing #user.first_name and .last_name as the value. I was thinking more something more like this:
<div class="field">
<%= f.label "First Name" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label "Last Name" %><br />
<%= f.text_field :name %>
</div>
where there is no #user.first_name but #user.name[0]
You can use text_field_tag to achieve this. (documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag)
A simple example would look like:
text_field_tag 'name[]', #user.first_name
text_field_tag 'name[]', #user.last_name
This will send both names under the name parameter. You can access the array with params[:name] in your controller.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Associated model is not saving data when page is refreshed
I have a 1:1 association between User and Profile.
When I submit the new profile form, the data I've entered is displayed just fine (see screenshot: http://i.imgur.com/fY8YU.png), but when I refresh it the data is instantly wiped.
Could anyone tell me what is causing this?
Here's the submit form:
<%= form_for([#user, #user.build_profile]) do |f| %>
<div
class="field"> <%= f.label :first_name %><br /> <%=
f.text_field :first_name %> </div> <div
class="field"> <%= f.label :last_name %><br /> <%=
f.text_field :last_name %> </div> <div
class="field"> <%= f.label :picture %><br /> <%=
f.text_field :picture %> </div> <div class="field">
<%= f.radio_button(:sex, "male") %> <%=
f.label(:sex, "Male") %> <%= f.radio_button(:sex, "female")
%> <%= f.label(:sex, "Female") %> </div> <div
class="actions"> <%= f.submit %> </div> <%
end %>
Here's the users_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/users_controller.rb
Here's the profiles_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/profiles_controller.rb
Basically, it's failing because:
passing #user.build_profile instantiates a new profile each time.
sidenote but I highly recommend you create the profile just after the user is created.
Look at my commit on your forked repository.
Rails 3.1 RC4
I have a 1:1 association between User and Profile.
When I submit the new profile form, the data I've entered is displayed just fine (see screenshot: http://i.imgur.com/fY8YU.png), but when I refresh it the data is instantly wiped.
Could anyone tell me what is causing this?
Here's the submit form:
<%= form_for([#user, #user.build_profile]) do |f| %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :picture %><br />
<%= f.text_field :picture %>
</div>
<div class="field">
<%= f.radio_button(:sex, "male") %>
<%= f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female") %>
<%= f.label(:sex, "Female") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here's the users_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/users_controller.rb
Here's the profiles_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/profiles_controller.rb
I'm not sure I agree with your approach. Why don't you do something like this:
In models/user.rb:
accepts_nested_attributes_for :profile
In controllers/users_controller.rb:
def new
#user = User.new
#user.build_profile
end
In views/users/_form.html.erb:
<%= form_for #user do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :profile do |pf| %>
<%= pf.text_field :some_profile_field %>
<% end -%>
<%- end -%>
This isn't copied or tested, but it should work. On saving your user, the profile fields are sent along and validated with the user fields and are re-rendered when rendering the form again after a save error. This way you will keep full control over your form and its contents with minimal effort.