作业中的NoMethodError#show

时间:2016-11-03 17:53:45

标签: ruby-on-rails ruby

我整天都被困在这一天。当我尝试在我的ruby on rails项目中显示作业的详细信息,创建作业或编辑当前作业时,我在第二行的第一行中显示作业中的NoMethodError,说明编辑链接存在问题页。

工作/ show.html

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

<p>
  <strong>Name:</strong>
  <%= @job.name %>
</p>

<p>

  <strong>Employer:</strong>
  <%= @job.employer %>
</p>


<p>
  <strong>Sector:</strong>
  <%= @job.sector_id %>
</p>

<p>
  <strong>Experience req:</strong>
  <%= @job.experience_req %>
</p>

<p>
  <strong>Job info:</strong>
  <%= @job.job_info %>
</p>

<h2>Star comment: </h2>
<%=form_for([@job, Request.new]) do |f| %>

</h3></br>
 <%= f.text_area:content, :rows => 4, :cols=> 40%>
 <div class = "actions">
     <%=f.submit "Make a request for the job"%>
 </div>
<% end %>

<%if @job.requests.empty? %>
  <h3> You are the first to Request</h3>
<% else %>
   <h2> Who else had made a request for this job:</h2>
   <% @job.requests.reverse.each do |request| %>
     <p><%= request.content %>
     Posted <%=time_ago_in_words(request.created_at)%> ago by
    <%=request.candidate.can_name%></p>
<% end %>
<% end %>

 <%= link_to 'Edit', edit_jobs_path(@job) %> | **This line highlights an error**
 <%= link_to 'Back', jobs_path %>

工作/ edit.html

<h1>Editing job</h1>

<%= render 'form' %>

<%= link_to 'Show', @job %> |
<%= link_to 'Back', jobs_path %>

路线

Rails.application.routes.draw do
  # get 'sessions/new'
  # get 'sessions/create'
  #get 'sessions/destroy'

controller :sessions do
  get 'login' =>:new
  post 'login' =>:create
  get 'logout' =>:destroy
  delete 'logout' =>:destroy
end

#get 'pages/home'
#get 'pages/about'

resources :candidates
resources :requests
resources :employers
resources :jobs
resources :sectors



 # The priority is based upon order of creation: first created -> highest      
 # priority.
 # See how all your routes lay out with "rake routes".

 # You can have the root of your site routed with "root"
   root 'pages#home'
   #root :to=>'pages#home
   #'welcome#index'

 resources :jobs do
  resources :requests
   end
  end

Jobs_Controller

class JobsController < ApplicationController
 before_action :set_job, only: [:show, :edit, :update, :destroy]

 # GET /jobs
 # GET /jobs.json
 def index
  @jobs = Job.all
 end

 # GET /jobs/1
 # GET /jobs/1.json
 def show
 end

 # GET /jobs/new
 def new
  @job = Job.new
 end

  # GET /jobs/1/edit
  def edit
   end

  # POST /jobs
  # POST /jobs.json
  def create
  @job = Job.new(job_params)

  respond_to do |format|
  if @job.save
     format.html { redirect_to @job, notice: 'Job was successfully created.'      
  }
      format.json { render :show, status: :created, location: @job }
    else
      format.html { render :new }
      format.json { render json: @job.errors, status: :unprocessable_entity    
  }
   end
 end
end

# PATCH/PUT /jobs/1
# PATCH/PUT /jobs/1.json
def update
respond_to do |format|
    if @job.update(job_params)
     format.html { redirect_to @job, notice: 'Job was successfully updated.'       
}
      format.json { render :show, status: :ok, location: @job }
    else
     format.html { render :edit }
     format.json { render json: @job.errors, status: :unprocessable_entity }
    end
  end
end

 # DELETE /jobs/1
 # DELETE /jobs/1.json
 def destroy
 @job.destroy
 respond_to do |format|
   format.html { redirect_to jobs_url, notice: 'Job was successfully   
  destroyed.' }
  format.json { head :no_content }
 end
end

private
  # Use callbacks to share common setup or constraints between actions.
 def set_job
   @job = Job.find(params[:id])
 end

  # Never trust parameters from the scary internet, only allow the white     
  #list through.
    def job_params
   params.require(:job).permit(:name, :employer, :sector_id,   
   :experience_req,:job_info)
   end
  end

更新

Ursus告诉我,我必须在<%= link_to 'Show', @job %>中将<%= link_to 'Show', job_path(@job) %>更改为Jobs/edit.html。我这样做了,但是当我尝试创建一个新工作或编辑当前工作时,我仍然得到相同的错误,但仍然创建了新工作?

1 个答案:

答案 0 :(得分:2)

这个

<%= link_to 'Show', @job %>

应该是

<%= link_to 'Show', job_path(@job) %>