我已閱讀以前有關此錯誤的問題的答案,但似乎無法使其適應當前情況。ActionController :: UrlGenerationError ...缺少必需的鍵:[:id]
我通過TUTS + Riding Ruby on Rails課程的國際化部分工作(比較我的代碼在course repo什麼的),並進行演練,我不得不默認區域設置更改爲葡萄牙(PT),並更改路線,以便我的問題和項目資源由語言環境(例如localhost3000/pt/issues/14
)作爲前綴,在我的路線文件中,範圍在/:locale
之下。
似乎由於某種原因ID密鑰正在被區域設置哈希取代。當我提交新問題時,出現以下錯誤:
ActionController::UrlGenerationError in Issues#show
Showing /Users/andrekibbe/code/new_issues/app/views/issues/show.html.erb where line #19 raised:
No route matches {:action=>"show", :controller=>"projects", :id=>nil, :locale=>#<Project id: 1, name: "First project", description: "A little description", created_at: "2015-06-20 15:34:58", updated_at: "2015-06-20 15:34:58">} missing required keys: [:id]
我在做什麼錯?我有正確的路徑名嗎?
提取的源(圍繞線#19):
問題/ show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @issue.title %>
</p>
<p>
<strong>Description:</strong>
<%= @issue.description %>
</p>
<p>
<strong>No followers:</strong>
<%= @issue.no_followers %>
</p>
<p><b>Project: </b>
<%= link_to @issue.project.name, @issue.project %></p>
<%= link_to 'Edit', edit_issue_path(@issue) %> |
<%= link_to 'Back', issues_path %>
這裏有問題的create
行動和項目控制器:
issues_controller.rb
def create
@issue = Issue.new(issue_params)
respond_to do |format|
if @issue.save
format.html { redirect_to @issue, notice: t('issues.created') }
format.json { render :show, status: :created, location: @issue }
else
format.html { render :new }
format.json { render json: @issue.errors, status: :unprocessable_entity }
end
end
end
projects_controller.rb
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
我的問題控制器的顯示方法,要求通過@Pavan,是Rails默認。這裏看不到太多。
issues_controller.rb
def show
end
我的路線:
的routes.rb
Rails.application.routes.draw do
scope "/:locale" do
get 'timeline/index'
resources :projects
resources :issues
end
end
耙路線
timeline_index GET /:locale/timeline/index(.:format) timeline#index
projects GET /:locale/projects(.:format) projects#index
POST /:locale/projects(.:format) projects#create
new_project GET /:locale/projects/new(.:format) projects#new
edit_project GET /:locale/projects/:id/edit(.:format) projects#edit
project GET /:locale/projects/:id(.:format) projects#show
PATCH /:locale/projects/:id(.:format) projects#update
PUT /:locale/projects/:id(.:format) projects#update
DELETE /:locale/projects/:id(.:format) projects#destroy
issues GET /:locale/issues(.:format) issues#index
POST /:locale/issues(.:format) issues#create
new_issue GET /:locale/issues/new(.:format) issues#new
edit_issue GET /:locale/issues/:id/edit(.:format) issues#edit
issue GET /:locale/issues/:id(.:format) issues#show
PATCH /:locale/issues/:id(.:format) issues#update
PUT /:locale/issues/:id(.:format) issues#update
DELETE /:locale/issues/:id(.:format) issues#destroy
我改變了路徑那些@nathanvda建議,但建議從內部Rails框架代碼本身產生以下錯誤消息:
NoMethodError in IssuesController#index
undefined method `set_locale' for #<IssuesController:0x007fd2f9fc2460>
Extracted source (around line #432):
case filter
when Symbol
lambda { |target, _, &blk| target.send filter, &blk }
when String
l = eval "lambda { |value| #{filter} }"
lambda { |target, value| target.instance_exec(value, &l) }
請更新您的帖子,使用'issues_controller'的'show'方法 – Pavan
@Pavan已添加,但正如我在編輯中提到的那樣,它只是默認方法,除了它繼承的方法外,它是空的。 – Gameboy70
嘗試給它像這樣''%= link_to @ issue.project.name,@ issue.project.id%>' – Pavan