为什么我收到错误 "undefined method `id' for nil:NilClass",即使 Rails 中有一个 ID?
Why am I getting error "undefined method `id' for nil:NilClass" even though there's a id in Rails?
您好,我正在执行 Rails 邮件操作。
routes.rb:
resources :textbook_giveaways do
member do
get 'contact', to: 'textbook_giveaways#new_contact', as: 'new_contact'
post 'contact', to: 'textbook_giveaways#send_contact', as: 'send_contact'
end
end
new_contact_html.erb:
<div class="container">
<div class="row">
<h2>ID is: <%= @textbook_giveaway.id %></h2>
<form role="form" action="" method="post" >
<!--<div class="col-sm-6">-->
<!-- <div class="col-sm-offset-4 col-sm-4 col-sm-offset-4"> -->
<div class="col-sm-offset-4 col-sm-4 col-sm-offset-4">
<div class="form-group">
<!--<label for="InputMessage">Message</label>-->
<div class="input-group">
<%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>
<label><h4>I would like to buy <strong><%=@texbook_giveaway.title%></strong></h4></label>
这一行 <h2>ID is: <%= @textbook_giveaway.id %></h2>
工作正常。它显示了身份证号码。
但是 <%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>
这给了我错误。
picture
以上是错误图片
当我 运行 耙路线时,我得到:
new_contact_textbook_giveaway GET /textbook_giveaways/:id/contact(.:format) textbook_giveaways#new_contact
send_contact_textbook_giveaway POST /textbook_giveaways/:id/contact(.:format) textbook_giveaways#send_contact
textbook_giveaways GET /textbook_giveaways(.:format) textbook_giveaways#index
POST /textbook_giveaways(.:format) textbook_giveaways#create
new_textbook_giveaway GET /textbook_giveaways/new(.:format) textbook_giveaways#new
edit_textbook_giveaway GET /textbook_giveaways/:id/edit(.:format) textbook_giveaways#edit
textbook_giveaway GET /textbook_giveaways/:id(.:format) textbook_giveaways#show
PATCH /textbook_giveaways/:id(.:format) textbook_giveaways#update
PUT /textbook_giveaways/:id(.:format) textbook_giveaways#update
DELETE /textbook_giveaways/:id(.:format) textbook_giveaways#destroy
[1]: http://i.stack.imgur.com/MbiUz.png
我的控制器是这样的:
class TextbookGiveawaysController < ApplicationController
before_action :set_textbook_giveaway, only: [:show, :edit, :update, :destroy, :new_contact, :send_contact]
# GET /textbook_giveaways
# GET /textbook_giveaways.json
def index
@textbook_giveaways = TextbookGiveaway.all.order(created_at: :desc).paginate(page: params[:page], per_page: 10)
end
# GET /textbook_giveaways/1
# GET /textbook_giveaways/1.json
def show
end
# GET /textbook_giveaways/new
def new
@textbook_giveaway = TextbookGiveaway.new
end
# GET /textbook_giveaways/1/edit
def edit
if not @textbook_giveaway.user_email == current_user.email || current_user.email == "codeinflash@gmail.com"
redirect_to @textbook_giveaway
end
end
# POST /textbook_giveaways
# POST /textbook_giveaways.json
def create
@textbook_giveaway = TextbookGiveaway.new(textbook_giveaway_params)
@textbook_giveaway.user_email = current_user.email
@textbook_giveaway.giveaway = true
respond_to do |format|
if @textbook_giveaway.save
format.html { redirect_to @textbook_giveaway }
flash[:success] = "Textbook giveaway was successfully created."
else
format.html { render :new }
format.json { render json: @textbook_giveaway.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /textbook_giveaways/1
# PATCH/PUT /textbook_giveaways/1.json
def update
respond_to do |format|
if @textbook_giveaway.update(textbook_giveaway_params)
format.html { redirect_to @textbook_giveaway }
flash[:success] = 'Textbook giveaway was successfully updated.'
else
format.html { render :edit }
format.json { render json: @textbook_giveaway.errors, status: :unprocessable_entity }
end
end
end
# DELETE /textbook_giveaways/1
# DELETE /textbook_giveaways/1.json
def destroy
@textbook_giveaway.destroy
respond_to do |format|
format.html { redirect_to textbook_giveaways_url }
format.json { head :no_content }
flash[:alert] = "Textbook giveaway was successfully destroyed."
end
end
#here for contact
def new_contact
end
def send_contact
message = params[:message]
if Contact.send_contact(@textbook_giveaway, current_user, message).deliver
flash[:success] = "Email sent."
redirect_to @textbook_giveaway
else
flash[:alert] = "There was a problem sending the email."
render :new_contact
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_textbook_giveaway
@textbook_giveaway = TextbookGiveaway.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def textbook_giveaway_params
params.require(:textbook_giveaway).permit(:title, :subject, :user_email, :description, :giveaway, :slug, :thumbnail)
#params.fetch(:textbook_giveaway, {})
end
end
这是您代码中的一个小错字。
这一行是正确的:
<h2>ID is: <%= @textbook_giveaway.id %></h2>
在下面这行中,您拼错了变量名。 @texbook_giveaway
应该是 @textbook_giveaway
.
<%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>
在 Ruby 中调用未分配的实例变量不会引发错误。它只是 returns nil
.
您好,我正在执行 Rails 邮件操作。
routes.rb:
resources :textbook_giveaways do
member do
get 'contact', to: 'textbook_giveaways#new_contact', as: 'new_contact'
post 'contact', to: 'textbook_giveaways#send_contact', as: 'send_contact'
end
end
new_contact_html.erb:
<div class="container">
<div class="row">
<h2>ID is: <%= @textbook_giveaway.id %></h2>
<form role="form" action="" method="post" >
<!--<div class="col-sm-6">-->
<!-- <div class="col-sm-offset-4 col-sm-4 col-sm-offset-4"> -->
<div class="col-sm-offset-4 col-sm-4 col-sm-offset-4">
<div class="form-group">
<!--<label for="InputMessage">Message</label>-->
<div class="input-group">
<%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>
<label><h4>I would like to buy <strong><%=@texbook_giveaway.title%></strong></h4></label>
这一行 <h2>ID is: <%= @textbook_giveaway.id %></h2>
工作正常。它显示了身份证号码。
但是 <%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>
这给了我错误。
picture 以上是错误图片
当我 运行 耙路线时,我得到:
new_contact_textbook_giveaway GET /textbook_giveaways/:id/contact(.:format) textbook_giveaways#new_contact
send_contact_textbook_giveaway POST /textbook_giveaways/:id/contact(.:format) textbook_giveaways#send_contact
textbook_giveaways GET /textbook_giveaways(.:format) textbook_giveaways#index
POST /textbook_giveaways(.:format) textbook_giveaways#create
new_textbook_giveaway GET /textbook_giveaways/new(.:format) textbook_giveaways#new
edit_textbook_giveaway GET /textbook_giveaways/:id/edit(.:format) textbook_giveaways#edit
textbook_giveaway GET /textbook_giveaways/:id(.:format) textbook_giveaways#show
PATCH /textbook_giveaways/:id(.:format) textbook_giveaways#update
PUT /textbook_giveaways/:id(.:format) textbook_giveaways#update
DELETE /textbook_giveaways/:id(.:format) textbook_giveaways#destroy
[1]: http://i.stack.imgur.com/MbiUz.png
我的控制器是这样的:
class TextbookGiveawaysController < ApplicationController
before_action :set_textbook_giveaway, only: [:show, :edit, :update, :destroy, :new_contact, :send_contact]
# GET /textbook_giveaways
# GET /textbook_giveaways.json
def index
@textbook_giveaways = TextbookGiveaway.all.order(created_at: :desc).paginate(page: params[:page], per_page: 10)
end
# GET /textbook_giveaways/1
# GET /textbook_giveaways/1.json
def show
end
# GET /textbook_giveaways/new
def new
@textbook_giveaway = TextbookGiveaway.new
end
# GET /textbook_giveaways/1/edit
def edit
if not @textbook_giveaway.user_email == current_user.email || current_user.email == "codeinflash@gmail.com"
redirect_to @textbook_giveaway
end
end
# POST /textbook_giveaways
# POST /textbook_giveaways.json
def create
@textbook_giveaway = TextbookGiveaway.new(textbook_giveaway_params)
@textbook_giveaway.user_email = current_user.email
@textbook_giveaway.giveaway = true
respond_to do |format|
if @textbook_giveaway.save
format.html { redirect_to @textbook_giveaway }
flash[:success] = "Textbook giveaway was successfully created."
else
format.html { render :new }
format.json { render json: @textbook_giveaway.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /textbook_giveaways/1
# PATCH/PUT /textbook_giveaways/1.json
def update
respond_to do |format|
if @textbook_giveaway.update(textbook_giveaway_params)
format.html { redirect_to @textbook_giveaway }
flash[:success] = 'Textbook giveaway was successfully updated.'
else
format.html { render :edit }
format.json { render json: @textbook_giveaway.errors, status: :unprocessable_entity }
end
end
end
# DELETE /textbook_giveaways/1
# DELETE /textbook_giveaways/1.json
def destroy
@textbook_giveaway.destroy
respond_to do |format|
format.html { redirect_to textbook_giveaways_url }
format.json { head :no_content }
flash[:alert] = "Textbook giveaway was successfully destroyed."
end
end
#here for contact
def new_contact
end
def send_contact
message = params[:message]
if Contact.send_contact(@textbook_giveaway, current_user, message).deliver
flash[:success] = "Email sent."
redirect_to @textbook_giveaway
else
flash[:alert] = "There was a problem sending the email."
render :new_contact
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_textbook_giveaway
@textbook_giveaway = TextbookGiveaway.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def textbook_giveaway_params
params.require(:textbook_giveaway).permit(:title, :subject, :user_email, :description, :giveaway, :slug, :thumbnail)
#params.fetch(:textbook_giveaway, {})
end
end
这是您代码中的一个小错字。
这一行是正确的:
<h2>ID is: <%= @textbook_giveaway.id %></h2>
在下面这行中,您拼错了变量名。 @texbook_giveaway
应该是 @textbook_giveaway
.
<%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>
在 Ruby 中调用未分配的实例变量不会引发错误。它只是 returns nil
.