دنبال کننده ها

۱۳۹۶ مهر ۱۱, سه‌شنبه

ruby on rails - ActionController::UrlGenerationError in UsersController#create ... missing required keys: [:id], possible unmatched constraints: [:locale]

[ad_1]



I had some nice form that had been working well, but once I add some translations I get following error:



Error Message



Here are some important files I have:



routes.rb



Rails.application.routes.draw do
scope ":locale", locale: /#")/ 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


users/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 %>
...


users/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
...


I spent whole day reading stackoverflow, I18n guides and watching youtube videos. I'm just a beginner. Please support.




[ad_2]

لینک منبع