Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8435939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:03:33+00:00 2026-06-10T07:03:33+00:00

I’ve got a pickem object that has one result. I’m having an issue getting

  • 0

I’ve got a pickem object that has one result. I’m having an issue getting the result to save properly to the database.

pickems_controller.rb

def results
 @pickem = Pickem.find params[:id]
 # @pickem.result = @pickem.build_result if @pickem.result.blank?
 @pickem.result = Result.new
end

def update_results
 @pickem = Pickem.find params[:id]
 @pickem.result = Result.new params[:pickem][:result_attributes]

 if @pickem.result.update_attributes params[:pickem][:result_attributes]
  redirect_to edit_pickem_results_path(@pickem), :notice => 'The results have been successfully updated.'
 else
  render "edit"
 end
end

results.html.erb

<%= simple_form_for @pickem, :url => edit_pickem_results_path(@pickem), :method => :put,  do |f| %>

 <%= f.simple_fields_for :result do |r| %>
  <%= r.input :first_name %>
  ...
 <% end %>

 <%= f.submit :class => 'btn btn-success', :label => 'Submit Results' %>

<% end %>

pickem.rb

has_one :result, :dependent => :destroy
accepts_nested_attributes_for :result

result.rb

belongs_to :pickem

I was initially using the build_result code that is commented out in the controller but had to back out of that. With build_result a result record was saved to the database the instant somebody clicked into the results form. There are rules in place in the application that don’t allow users to make any picks if a result has been entered. So even if a user clicked into the result form but didn’t submit, the result was still being created.

How can I build my form and save the result record only when the user clicks save, and not when the form is loaded? The current solution I’ve pasted above does not work. It saves a result record with the appropriate foreign key but never gets the form data. If I dump @pickem.result the correct form data is in the result object, I just can’t get it to save right. Other solutions I’ve tried save the form data correctly but have a foreign key of 0.

EDIT:
For whatever reason @pickem.result = Result.new was still saving a record to the database so I changed it to @result = Result.new and updated the form as follows:

<%= simple_form_for @result, :url => edit_pickem_results_path(@pickem), :method => :put,  do |r| %>

 <%= r.input :first_name %>
 <%= r.submit :class => 'btn btn-success', :label => 'Submit Results' %>

<% end %>

Then using the suggestion from Chuck W of @result = @pickem.result.build params[:result], I get undefined methodbuild’ for nil:NilClass`.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:03:34+00:00Added an answer on June 10, 2026 at 7:03 am

    pickems_controller.rb

    def results
      @pickem = Pickem.find params[:id]
      @pickem.result.blank? ? @result = Result.new : @result = @pickem.result
    end
    
    
    def update_results
      @pickem = Pickem.find params[:id]
      @result = @pickem.result.build params[:pickem][:result]   
      if @result.save
       redirect_to edit_pickem_results_path(@pickem), :notice => 'The results have been successfully updated.'
      else
       render "edit"
      end
    end
    

    Then, your view should look something like this:

    <%= simple_form_for @pickem, :url => edit_pickem_results_path(@pickem), :method => :put,  do |f| %>
    
     <%= f.simple_fields_for @result do |r| %>
      <%= r.input :first_name %>
      ...
     <% end %>
    
     <%= f.submit :class => 'btn btn-success', :label => 'Submit Results' %>
    
    <% end %>
    

    You might have to play around with how the parameters are being passed back to the update_results action (I’m pretty new to rails), but I think you get the gist of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.