我在我的用戶文件夾(mypage.html.erb
)下創建了一個新頁面,因爲其上的內容需要用戶數據。我創建一個表單/下拉菜單,以及用於在把下面的代碼在某種原因,我拋出這個錯誤:用戶中的參數錯誤#pagename - 表單中的第一個參數不能包含零或爲空
undefined local variable or method `user' for #<#<Class:0x000000021aee18>:0x007fc52025c8e8>
<%= form_for(@user) do |f| %>` `<%= user.firstname %>` `<%= user.lastname %>`
我不知道爲什麼,因爲我知道@user
定義。我創建了多個表單並在其他自定義頁面上拉取了其他用戶數據。
見代碼:
mypage.html.erb
<div id="widepage">
<div class="blue-text-header">Title</div>
<div class="grey-text-subheader">Subtitle</div>
<div class="infoheaders">Information</div>
<%= form_for @user do |f| %>
<%= f.collection_select :questions, User::QUESTIONS, :to_s, :to_s,
:include_blank => true
%>
<%= f.submit "Create" %>
<% end %>
</div>
<div class="fullcard">
<div class="name">
<%= user.firstname %> <%= user.lastname %>
</div>
</div>
user.rb
class User < ActiveRecord::Base
def self.search(search)
where("firstname LIKE ? OR lastname LIKE ?", "%#{search}%", "%#{search}%")
end
QUESTIONS = ['hot', 'medium', 'cold']
has_secure_password
validates_length_of :password, :in => 6..20, :on => :create
validates :password_confirmation, presence: true, if: -> { new_record? || changes["password"] }
has_attached_file :avatar,
:path => ":rails_root/public/system/:attachment/:id/:basename_:style.:extension",
:url => "/system/:attachment/:id/:basename_:style.:extension",
:styles => {
:thumb => ['150x150#', :jpg, :quality => 100],
:preview => ['175x175#', :jpg, :quality => 70],
:large => ['600>', :jpg, :quality => 70],
:retina => ['1200>', :jpg, :quality => 30]
},
:convert_options => {
:thumb => '-set colorspace sRGB -strip',
:preview => '-set colorspace sRGB -strip',
:large => '-set colorspace sRGB -strip',
:retina => '-set colorspace sRGB -strip -sharpen 0x0.5'
}
validates_attachment :avatar,
:presence => true,
:size => { :in => 0..10.megabytes },
:content_type => { :content_type => /^image\/(jpeg|png|gif|tiff)$/ }
end
users_controller.rb
class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "You signed up successfully"
flash[:color] = "valid"
redirect_to @user
else
flash[:notice] = "Form is invalid"
flash[:color] = "invalid"
render "new"
end
end
def mypage
@user = User.new
end
def index
@user = User.all
if params[:search]
@user = User.search(params[:search]).order("created_at DESC")
else
@user = User.all.order('created_at DESC')
end
end
def show
@user = User.find(params[:id])
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:firstname, :lastname, :email, :aptno, :streetaddress, :city, :country, :stateprov, :poszip, :receive_newsletters, :terms_accepted, :password, :password_confirmation, :avatar, :bio)
end
# def user_params
# params.require(:user).permit(:name, :description, :avatar)
# end
def new
end
end
如果您將mypage.html.erb文件的名稱更改爲show.html.erb或index.html.erb,希望您的操作是users_controller – Dias
中的「我的頁面」,那麼您仍然收到錯誤消息嗎? –
我看到'#new'在'private'下 - 知道private關鍵字,除非內聯方法定義,否則將以下所有方法/ etc標記爲PRIVATE。這意味着'new'是私人的,當它不應該是。 – onebree