1

I have two models: Schedules and Result.

  • Schedule has_one Result
  • Result belongs_to Schedule

Schedules data will be created first and as matches happen, we will create the results for each schedule.

enter image description here

As in the picture above, the Create link will take you to a page where you will create the result for the schedule. I will send the schedule_id of the schedule for which Create button is clicked.

<%= link_to "Create",new_result_url(:schedule_id => schedule.id),{:class => 'btn btn-link btn'}%>

And in the Results#New

  def new
    @schedule = Schedule.find(params[:schedule_id])
    @result = @schedule.build_result
  end

And in the View results/new.html.erb

This is where I am stuck or dont know how to submit the result form for the schedule_id I selected

<div class="row">
  <div class="col-md-4">

<%= form_for(@result) do |f| %>
    <h3>Enter the result</h3>
    <%= f.text_area :result,class:'form-control' %><br />
    <%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>

2 Answers 2

3

You might want to nest result under schedules. In your routes:

resources :schedules do
  resource :result
end

And then your controller could look something like this:

class ResultsController < ApplicationController
  def create
    schedule.create_result(result_params)
  end

  private

  def schedule
    Schedule.find(params[:schedule_id])
  end

  def result_params
    params.require(:result).permit(:result)
  end
end

This architects your application to reflect your actual information architecture, and you don't have to worry about passing ids through hidden fields.

Sign up to request clarification or add additional context in comments.

Comments

1

In your form, add:

<%= f.hidden_field :schedule_id, value: @schedule.id %>

This will pass the id of the parent schedule in with your params. Also, make sure that you permit the parameter schedule_id in your controller.

Also, to make it easier to pass the schedule_id to the results#new page, I'd change the routes file to this:

resources :schedules do
  resources :results
end

That way, the route to the results#new page is now new_schedule_result_path(@schedule), which you can use in your link_to.

Edit:

Also, change your form_for to:

<%= form_for[@schedule, @result] do |f| %>

You would need to have @schedule defined in your controller.

4 Comments

Thank you Ryan and Jon Evans for your prompt answers, really appreciate !! I will try out and post the results soon !!
So in the result/new.html.erb, when I submit the form, it says undefined method results_path.. how do I build the form <%= form_for @result do |f| %> ?? or as its nested i should do some like this <%= form_for @schedule.result do |f| %> ?? sorry for being so naive in this, I am new to rails.
Ah, change your form_for to: <%= form_for [@schedule, @result] do |f| %>. Of course, you should have @schedule defined in your controller.
It worked !! Ryan, Jon you both are my stars today for helping out. I have been scratching my head for few days now. And learned a lot just in this post !! Actually I used combination of both of your answers, But can only mark one as answer .....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.