我已经阅读过有关此错误的问题的先前答案,但我似乎无法使其适应我目前的情况。
我正在完成Tuts + Riding Ruby on Rails课程的i18N部分(将我的代码与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行):
<p><b>Project: </b>
<%= link_to @issue.project.name, @issue.project %></p>
<%= link_to 'Edit', edit_issue_path(@issue) %> |
<%= link_to 'Back', issues_path %>
问题/ 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要求的我的问题控制器的show方法是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) }
答案 0 :(得分:2)
由于您在区域设置下设置了所有内容,因此rails路径助手假定第一个帮助器是区域设置。
如果您按如下方式编写链接,那么您应该很好:
link_to @issue.project.name, [I18n.locale, @issue.project]
但这似乎有点单调乏味。更好的是来自I18n guides的提示:将区域设置添加到默认网址选项,将以下方法添加到ApplicationController
:
def default_url_options(options = {})
{ locale: I18n.locale }.merge options
end
然后你的链接应该按原样运行。
答案 1 :(得分:0)
与我相同。旧代码是
<%= link_to "Click Here to reset your password", edit_password_reset_url(@user.password_reset_token), target: :_blank %>
我通过此更新对其进行了修复
<%= link_to "Click Here to reset your password", edit_password_reset_url(I18n.locale, @user.password_reset_token), target: :_blank %>
感谢@nathanvda