Dustin James
Dustin James

Reputation: 571

Passing an ID to controller show method in Rails

I have the following view page in my test_app:

<p id="notice"><%= notice %></p>

<p>
  <strong>Box count:</strong>
  <%= @job.box_count %>
</p>

<p>
  <strong>Install date:</strong>
  <%= @job.install_date %> 
</p>

<%= link_to "Back", :back %>

I have the following ruby code in my jobs_controller show method:

def show
  @job = Job.find(params[:job_id])
end

The parameters being passed through to the view are ("customer_id" and "id" for the job) as follows:

{"customer_id"=>"1", "id"=>"23"}

When I load the view for any job, I get the following error:

Couldn't find Job without an ID

I must have an error in my show method, but I am unsure exactly what I am doing wrong.

Any ideas?


It looks as though the issue is that my form is not saving the data to the table, here is my form:

<%= form_for([@customer, @job]) do |f| %>
  <% if @job.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@job.errors.count, "error") %> prohibited this job from being saved:</h2>

      <ul>
      <% @job.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :box_count %><br>
    <%= f.number_field :box_count %>
  </div>
  <div class="field">
    <%= f.label :install_date %><br>
    <%= f.text_field :install_date %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Any idea why the data is not being saved?

Upvotes: 0

Views: 1372

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

You should be using :id, not :job_id:

def show
  @job = Job.find(params[:id])
end

Upvotes: 2

Related Questions