Html 为类似Quora的Rails应用程序设置关联和表单
你好,我正在尝试使用RubyonRails构建一个类似quora的应用程序。在这种情况下,我决定把我的问题称为别针,我的答案仍然是答案。但是,当我试图回答一个问题时,我会出现以下错误:Html 为类似Quora的Rails应用程序设置关联和表单,html,ruby-on-rails,ruby,ruby-on-rails-4,nameerror,Html,Ruby On Rails,Ruby,Ruby On Rails 4,Nameerror,你好,我正在尝试使用RubyonRails构建一个类似quora的应用程序。在这种情况下,我决定把我的问题称为别针,我的答案仍然是答案。但是,当我试图回答一个问题时,我会出现以下错误: NameError at /pins/2 undefined local variable or method `answer' for #<#<Class:0x007fc934792fc0>:0x007fc93428fb18> 我也在使用Desive。以下是我迄今为止的联想 class
NameError at /pins/2 undefined local variable or method `answer' for #<#<Class:0x007fc934792fc0>:0x007fc93428fb18>
我也在使用Desive。以下是我迄今为止的联想
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :pins
has_many :answers
has_attached_file :image, :styles => { :medium => "400x400", :thumb => "200x200" }, : :default_url => "avatar/missing.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
这是我的Pin模型
class Pin < ActiveRecord::Base
belongs_to :user
validates :description, presence: true
has_many :answers
end
这是我的答案模型
class Answer < ActiveRecord::Base
belongs_to :pin
belongs_to :user
validates :description, presence: true
end
这是我的PIN控制器
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 50)
end
def show
end
def new
@pin = current_user.pins.build
end
def edit
end
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render action: 'new'
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params[:id])
end
def correct_user
@pin = current_user.pins.find_by(id: params[:id])
redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description)
end
end
这是我的答案
class AnswersController < ApplicationController
before_action :set_answer, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def create
@pin = Pin.find(params[:pin_id])
@answer = @pin.answers.create(answer_params)
redirect_to pin_path(@pin)
end
private
def set_answer
@answer = Answer.find(params[:id])
end
def correct_user
@answer = current_user.answers.find_by(id: params[:id])
redirect_to answers_path, notice: "Not authorized to edit this pin" if @answer.nil?
end
def answer_params
params.require(:answer).permit(:description)
end
end
这是我的表演表格
谁能告诉我这个问题吗。提前感谢。您看到了一个错误:
<% @pin.answers.each do |comment| %> # Here you have "comment"
<p>
<strong>Answer:</strong>
<%= answer.description %> # And here "answer", change it to "comment" or vice versa
</p>
<% end %>
<% @pin.answers.each do |comment| %> # Here you have "comment"
<p>
<strong>Answer:</strong>
<%= answer.description %> # And here "answer", change it to "comment" or vice versa
</p>
<% end %>