Я обнаружил ошибку в своем новом файле .html.erb в проекте загрузки файла журнала.Там написано "NoMethodError" и "неопределенный метод` name 'для # ".
Я не совсем понимаю, почему, потому что я уже определил имя в своей модели и контроллере.
My new.html.erb
<div class="container">
<% if @log_file_upload.errors.present? %>
<div>
<ul>
<% @log_file_upload.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form_for LogFileUpload.new, html: { multipart: true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<br><br>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<br>
<%= f.submit "Save" %>
<% end %>
</div>
</div>
Мой log_file_upload_controller.rb:
class LogFileUploadController < ApplicationController
def index
@log_file_upload = LogFileUpload.all
end
def new
@log_file_upload = LogFileUpload.new
end
def create
@log_file_upload = LogFileUpload.new(log_file_upload_params)
if @log_file_upload.save
redirect_to log_file_upload_path, notice: "Successfully uploaded."
else
render "new"
end
end
def destroy
@log_file_upload = LogFileUpload.find(params[:id])
@log_file_upload.destroy
redirect_to log_file_upload_path, notice: "Successfully deleted."
end
private
def log_file_upload_params
params.require(:log_file_upload).permit(:name, :attachment)
end
end
Мой route.rb:
Rails.application.routes.draw do
get 'log_file_upload/index'
get 'log_file_upload/new'
get 'log_file_upload/create'
get 'log_file_upload/destroy'
get 'sessions/new'
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "log_file_upload#index"
resources :users
resources :sessions
resources :log_file_uploads, :path => 'log_file_uploads', only: [:index, :new, :create, :destroy]
end
Мой log_file-upload.rb:
class LogFileUpload < ApplicationRecord
mount_uploader :attachment, AttachmentUploader
validates :names, presence: true
belongs_to :user
end
Мой user.rb
class User < ActiveRecord::Base
attr_accessor :password, :password_confirmation
before_save :encrypt_password
validates_presence_of :email
validates_uniqueness_of :email
validates_confirmation_of :password
validates_presence_of :password, :on => :create
has_many :log_file_uploads
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end