我有一些很好的表单,但是一旦我添加了一些翻译, 我会收到以下错误:
以下是我的一些重要文件:
的routes.rb
Rails.application.routes.draw do
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
get 'sessions/new'
get 'teamscrs/index'
get 'teamscrs/new'
get 'teamscrs/show'
get 'profile', to: 'teamscrs#show'
get 'login', to: 'sessions#new'
get 'users', to: 'users#new'
delete 'logout', to: 'sessions#destroy'
post 'login', to: 'sessions#create'
resources :users
root 'teamscrs#index'
end
match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), :via => [:get, :post]
match '', to: redirect("/#{I18n.default_locale}"), :via => [:get, :post]
#get '/teamscrs' => 'teamscrs#home'
end
users_controller.rb
class UsersController < ApplicationController
def index
end
def show
@user = User.find(params[:id])
end
def new
@user= User.new
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = t(".sukces")
redirect_to @user
else
flash.now[:danger] = t(".fail")
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_locale
helper_method :current_user, :logged_in?
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!!current_user
end
def must_login
if !logged_in?
flash[:danger] = t(".mustlogin")
redirect_to login_path
end
end
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_option(options = {})
{locale: I18n.locale}
end
end
用户/ new.html.erb
...
<%= form_with scope: :user, url: users_path, local: true do |form| %>
<div class="form-group">
<%= form.label(:name,t('.username'))%>
<%= form.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= form.label(:email,t('.email'))%>
<%= form.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= form.label(:password_digest,t('.pass'))%>
<%= form.password_field :password, class: 'form-control' %>
</div>
<div class="form-group">
<%= form.label(:password2, t('.passc'))%>
<%= form.password_field :password_confirmation, class: 'form-control' %>
</div>
<%= form.submit t('.join'), class: 'btn btn-success' %>
<% end %>
...
用户/ show.html.erb
<p>
<strong>Użytkownik:</strong>
<%= @user.name %>
</p>
<p>
<strong>email:</strong>
<%= @user.email %>
</p>
到config / environment.rb
...
Rails.application.configure do |variable|
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**/*.{rb,yml}').to_s]
I18n.available_locales = [:en, :pl]
I18n.default_locale = :pl
end
...
我花了一整天阅读stackoverflow,I18n指南和观看youtube视频。我只是一个初学者。请支持。
答案 0 :(得分:0)
我通过改变来解决它:
redirect_to @user
为:
redirect_to controller: 'users', action: 'show', id: @user.id
在我读完实现default_url_options的原因之后,我必须明确让Rails传递id