我在日志文件上传项目的new.html.erb上遇到错误。它显示“ NoMethodError”和“#的未定义方法'name'”。
我真的不明白为什么,因为我已经在模型和控制器中定义了名称。
我的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
我的路线.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
答案 0 :(得分:0)
您可以首先检查您的实际列名是什么。我注意到您在控制器中定义了“名称”并在模型中查看了“名称”。
顺便说一句,当您在控制器中定义LogFileUpload.new
时,您可以直接在视图中使用它,而不必两次调用新方法:
<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 @log_file_upload, 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>