submitted data vanishes after page refresh [duplicate] - ruby-on-rails

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.

Related

Rails: Form is not being loaded when trying to update

I am just trying to set up a basic application with Ruby on Rails. I have the basic CRUD functions for different entities and with one of them, the form that is supposed to load when the user wants to edit a row does not appear. It knows which ID it needs to edit, it just doesn't load the form.
Link to take you to edit page;
<%= link_to 'Edit Review'. edit_review_path(review) %>
Controller function;
def edit
#review=Review.find(params[:id])
end
HTML Form;
<div class="container">
<h2>Edit Review Details</h2>
<% form_for #review do |f| %>
<div class="form-group">
<%= f.label :"Profile" %><br />
<%= f.select :ProfileId, Profile.all.collect {|x| [x.Name,x.id]}, {:include_blank => 'Select Profile'}, class:'form-control'%> <br /><br />
</div>
<div class="form-group">
<%= f.label :"Product" %><br />
<%= f.select :ProductId, Product.all.collect {|x| [x.pName,x.id]}, {:include_blank => 'Select Product'}, class:'form-control'%>
</div>
<div class="form-group">
<%= f.label :"Review Author" %>
<%= f.text_field :Author %>
</div>
<div class="form-group">
<%= f.label :"Product Rating" %>
<%= f.number_field :ProductRating %>
</div>
<div class="form-group">
<%= f.label :"Review Text"%>
<%= f.text_field :ReviewText %>
</div>
<div class="form-group">
<%= f.label :"Date of Review" %>
<%= f.date_field :DateofReview %>
</div>
<div class="form-group">
<%= f.submit %>
</div>
<% end %>
</div>
I have 2 models that have a working update feature and they are the exact same code as this however for this, when I click the button. The page loads but nothing appears other than the text.
Can someone explain why this is happening? I thought it may be the dropdown boxes but I removed them and still no form loaded up.

undefined method for form fields

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.

Altering multiple rows into a model with one submit

I'm trying to put together a rails site that uses a number of models. One is a model called requests that takes in a couple pieces of information associated with essentially a single purchase. The other is a model that collects a number of the requests together under one "order" id.
The problem is, I want the view associated with request to allow for multiple request entries with one submission, and I can't figure that out. As I've currently got it, someone can submit a single request for a single "cut", but I want them to enter a number of requests for a number of cuts on a single page, with a single submission.
My current _form document associated with my view is:
<%= form_for(#request) do |f*| %>
<div class="field">
<%= f.label :cut %><br />
<%= f.text_field :cut %>
</div>
<div class="field">
<%= f.label :lbs %><br />
<%= f.number_field :lbs %>
</div>
<div class="field">
<%= f.label :notes %><br />
<%= f.text_field :notes %>
</div>
<div class="field">
<%= f.label "Order ID" %><br />
<%= f.number_field :order_id %>
</div>
<div class="field">
<%= f.label :status %><br />
<%= f.number_field :status %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Any idea how I can create a form document that allows me to enter several new 'rows' of requests with one submit? Or how to ensure they all have the same order-id?
Thanks.
Try these screencasts from Ryanb, I think they'll get you a long way.
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2

Edit form does not auto-fill recently added columns

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

Associated model is not saving data when page is refreshed

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.

Resources