我有一个关于 Rails 的问题。我的数据库中有一个没有主键的表,其中包含这些列 - person_id、year、salary。我的问题是当我想编辑其中一份薪水时。我不明白该怎么做。那是我的代码:
工资控制员:
def index
@salaries = Salary.all
end
def edit
@salary = Salary.find_by_person_id_and_year(params[:person_id], params[:year])
@people = Person.all
end
index.html.erb
<% @salaries.each do |salary| %>
<b><%= salary.person.Name %></b>
<b><%= salary.Year %></b>
<b><%= salary.Amount %></b>
<%= link_to "Edit", edit_salary_path(salary.person_id, salary.year)%>
<% end %>
所以,我将它添加到我的 routes.rb 中:
get 'salaries/index', :to => 'salaries#index',:as => 'salaries'
get 'salaries/:person_id/:year', :to => 'salaries#show', :as => 'salary'
get 'salaries/new/:person_id/:year', :to => 'salaries#edit', :as => 'new_salary'
get 'salaries/edit/:person_id/:year', :to => 'salaries#edit', :as => 'edit_salary'
put 'salaries/:person_id/:year', :to => 'salaries#update'
post 'salaries/:person_id/:year', :to => 'salaries#create'
delete 'salaries/:person_id/:year', :to => 'salaries#destroy'
但是我的 _form.html.erb 中有这些错误消息:“undefined method `join' for nil:NilClass” on this line: <%= form_for(@salary) do |f| %>
def edit
@person_id = params[:person_id]
@year = params[:year]
@salary = Salary.find_by_person_id_and_year(params[:person_id], params[:year])
@people = People.all
end
def update
@people = People.all
@salary = Salary.new(params[:salary])
if @salary.save
flash[:success] = "Successfully edited salary"
redirect_to salaries_path
else
@title = "Edit salary"
render 'edit'
end
end
_form.html.erb
<%= form_for(@salary, :url => salaries_path(:person_id => @person_id, :year => @year)) do |f| %>