Note that there are some explanatory texts on larger screens.

plurals
  1. POEdit method is creating new records, instead of just updating existing ones
    primarykey
    data
    text
    <p>My question is: How do I make the edit/update methods for my profiles controller stop creating new records when I edit a profile page? </p> <p>I have a user model and profile model.<br> <code>user has_one :profile</code>.<br> <code>profile belongs_to :user</code>.</p> <p>My routes.rb looks like this: map.resources :users, :has_one => :profile</p> <p>When a visitor to my app creates a user and hits submit, they are directed to the "create profile" screen.</p> <p>The following controller, creates a profile such that the created profile url is: localhost:3000/users/[user_id]/profile</p> <pre><code>def new @profile = `current_user.build_profile` end def create @profile = current_user.build_profile(params[:profile]) if @profile.save flash[:notice] = 'Profile was successfully created.' redirect_to user_profile_path else flash[:notice] = 'Error. Something went wrong.' render "new" end end </code></pre> <p>This all works fine.</p> <p>The problem occurs when I edit a profile. </p> <p>At first things seems fine. When I go to the "edit profile" view, the url is correct. For example, if the url for the "show profile" view is localhost:3000/users/1/profile, then the "edit profile" view appears as: localhost:3000/users/1/profile/edit</p> <p>That's good.</p> <p>When I make a change and click the update button, that seems to work as well. I get directed to the correct "show profile" view and I get a flash notice confirming the change.</p> <p>So here's the weird part. When I go back to my index of profiles to view all the profiles in the app, it turns out that the app has created a new profile record each time I update the profile. The original profile record is still there, but there are new additional records. Keep in mind that all the profiles in my app are tied to a user, and the user can have only one profile. So if there are 5 users in my app, now there might be 10 different profiles, each with a url that leads to a busted page because the user_id doesn't exist in the database. For example, localhost:3000/users/7/profile, users/8/profile, etc. </p> <p>The record gets saved to the profile table in my database, but the user_id column shows a NUL.</p> <p>I figure that the problem is coming from either (a) the edit/update methods in the profiles controller, or (b) the code I'm using to display profiles on my profile index page.</p> <p>Here's my profile controller for the edit and update methods:</p> <pre><code>def edit @profile = current_user.profile(params[:id]) end def update @profile = current_user.profile.find(params[:id]) if @profile.update_attributes(params[:profile]) flash[:notice] = 'Profile was successfully updated.' redirect_to(@profile) else render "edit" end </code></pre> <p>end</p> <p>Here's the profile index.html.erb view file:</p> <pre><code>&lt;div id="posts"&gt; &lt;% @profiles.each do |profile| -%&gt; &lt;% unless profile.blank? -%&gt; &lt;div class="profiles"&gt; &lt;div class="right"&gt; &lt;p&gt;&lt;%=h profile.name %&gt;&lt;/p&gt; &lt;p&gt;&lt;%=h profile.category %&gt;&lt;/p&gt; &lt;/div&gt; &lt;div class="bottom"&gt; &lt;p&gt;&lt;%= link_to 'See Profile', user_profile_path(profile) %&gt;&lt;/p&gt; &lt;/div&gt; &lt;% end -%&gt; &lt;/div&gt; &lt;% end -%&gt; </code></pre> <p></p> <p>To repeat my question: How do I make the edit/update methods for my profiles controller stop creating new records when I edit a profile page?</p> <h1>UPDATE</h1> <p>Looking at the log, it looks like it's calling the Edit method, but then calling the "create" method instead of the "update" method to execute the action. Here's what I'm getting:</p> <pre><code>Processing ProfilesController#edit (for IP at 2009-08-29 00:46:06) [GET] Parameters: {"action"=&gt;"edit", "controller"=&gt;"profiles", "user_id"=&gt;"1"} User Load (0.3ms) SELECT * FROM "users" WHERE ("users"."id" = 1) Profile Load (0.6ms) SELECT * FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1 Rendering template within layouts/application Rendering profiles/edit SQL (0.3ms) SELECT count(*) AS count_all FROM "posts" WHERE ("posts".profile_id = 20) Rendered users/_login (1.2ms) Completed in 29ms (View: 16, DB: 1) | 200 OK </code></pre> <p>[<a href="http://localhost/users/1/profile/edit]" rel="nofollow noreferrer">http://localhost/users/1/profile/edit]</a> </p> <pre><code>Processing ProfilesController#create (for IP at 2009-08-29 00:46:21) [POST] Parameters: {"commit"=&gt;"Update", "profile"=&gt;{"city"=&gt;"Brooklyn", "address1"=&gt;"Bedford Ave.", "name"=&gt;"Place", "zip"=&gt;"19876", "address2"=&gt;"Apt. 4", "category"=&gt;"Restaurant", "website"=&gt;"http://www.whatever.com", "description"=&gt;"Place is awesome.", "phone"=&gt;"555-1212", "email"=&gt;"place@whatever.com", "state"=&gt;"NY"}, "action"=&gt;"create", "authenticity_token"=&gt;"[redacted]", "controller"=&gt;"profiles", "user_id"=&gt;"1"} User Load (0.3ms) SELECT * FROM "users" WHERE ("users"."id" = 1) Profile Load (0.6ms) SELECT * FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1 Profile Update (0.6ms) UPDATE "profiles" SET "updated_at" = '2009-08-29 04:46:21', "user_id" = NULL WHERE "id" = 20 Profile Create (0.6ms) INSERT INTO "profiles" ("name", "address1", "city", "address2", "zip", "category", "updated_at", "website", "description", "category_id", "phone", "user_id", "state", "email", "created_at") VALUES('Place', 'Bedford Ave.', 'Brooklyn', 'Apt. 4', '19876', 'Restaurant', '2009-08-29 04:46:21', 'http://www.whatever.com', 'Place is awesome.', NULL, '555-1212', 1, 'NY', 'place@whatever.com', '2009-08-29 04:46:21') Redirected to http://localhost:3000/users/1/profile Completed in 45ms (DB: 2) | 302 Found [http://localhost/users/1/profile] </code></pre> <h2>UPDATE </h2> <p>I think this is the relevant part, but let me know if you need more. The rest is just the individual fields.</p> <pre><code>&lt;% @profile.posts.build if @profile.posts.empty? %&gt; &lt;% form_for :profile, @profile, :url =&gt; { :controller =&gt; 'profiles', :action =&gt; 'update' } do |profile_form| %&gt; &lt;%= profile_form.error_messages %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload