开发者

Basic Ruby On Rails Linking Help

开发者 https://www.devze.com 2022-12-24 15:32 出处:网络
So I am beginning to work with Rails and I get some of the concepts but am stuck on an important one.

So I am beginning to work with Rails and I get some of the concepts but am stuck on an important one.

Let's say I have customers which has many jobs and jobs which belongs to customers.

How would I go about creating a new job for a customer开发者_运维知识库?

I can create a link that goes to customers/1/jobs/new and I can grab the customer ID but how do I tell it that I am creating a job for customer 1?

I know this is the most basic of things but I just need a push in the right direction.

This is my form so far: How do I get :customer_id to populate with the customer_id param?

    <h1>New job</h1>

<% form_for(@job) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :customer_id %><br />
    <%= f.text_field :customer_id %>
  </p>
  <p>
    <%= f.label :manufacturer %><br />
    <%= f.text_field :manufacturer %>
  </p>
  <p>
    <%= f.label :serial_number %><br />
    <%= f.text_field :serial_number %>
  </p>
  <p>
    <%= f.label :problem %><br />
    <%= f.text_area :problem %>
  </p>
  <p>
    <%= f.label :notes %><br />
    <%= f.text_area :notes %>
  </p>
  <p>
    <%= f.label :status %><br />
    <%= f.text_field :status %>
  </p>
  <p>
    <%= f.label :tech_id %><br />
    <%= f.text_field :tech_id %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', jobs_path %>


Just use form_for([@customer, @job]), this should generate the correct URLs (/customers/:customer_id/jobs etc).

You can then fetch params[:customer_id] in your JobsController.create method.


Here's a long, maintained, descriptive list of good ruby programming tutorials. Good luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消