0

I am trying to ensure that each job has the correct link when a user clicks on it.

The issue I am having is that I keep getting the error Couldn't find job without an ID.

In my view I have the following code:

 <% @jobs.group_by{|x| x.created_at.strftime("%d %b. %Y")}.each do |date,jobs_on_that_date| %>
   <section class="date"><%= date %></section>
   <section id="top-job">
      <% jobs_on_that_date.each do |job| %>
   <section id="job-wrapper">
  <%= link_to url_with_protocol(@link.job_url), :target => '_blank' do %>
  <section id="job">
        <%= job.title %> - <%= job.company %>
        <%= job.salary %>
        <%= job.location %> 
        <%= job.job_type %>
  </section>
  </section>    
  <% end %>
  <% end %>
  </section>
  <% end %>

In my controller I am creating an object called @link as follows:

 def index
  @user = current_user
  @jobs = Job.where('created_at > ?', 30.days.ago).reverse
  @link = Job.find(params[:id])
 end

Finally I have the following Routes setup.

 JobappV2::Application.routes.draw do

   devise_for :users

   resources :jobs

   resources :users do
      resources :jobs
   end

   root :to => 'jobs#index'


   end

If I use @link = Job.find(2) this works but every job ends up with the link input by the job with id 2.

Thanks in advance for any help!


what is params[:id]?

I suspect that if you do raise params[:id].inspect in your controller, you will find it's nil. Besides, what's the point in creating @link? just put job.job_url in your view instead of @link.job_url

4

1 回答 1

1

什么是params[:id]

我怀疑如果你raise params[:id].inspect在你的控制器中这样做,你会发现它是nil. 此外,创作的意义@link何在?只是放在job.job_url你的视野中,而不是@link.job_url

于 2012-08-03T10:18:21.027 回答