我有几个模型has_many :attachments
。Rails redirect_to控制器中纠正页面
在创建Note
之后,我试图重定向回Note view
。
这是我正在尝试的附件控制器代码。 @note
告诉我这个附件与Note
有关。
# GET /attachments/new
# GET /attachments/new.json
def new
@attachment = Attachment.new
@comment = params[:comment_id]
@note = params[:note_id]
respond_to do |format|
format.html # new.html.erb
format.json { render json: @attachment }
end
end
# POST /attachments
# POST /attachments.json
def create
@attachment = Attachment.new(params[:attachment])
respond_to do |format|
if @attachment.save
if @note != nil
format.html { redirect_to note_path(@note), notice: 'Attachment was successfully created.' }
else
format.html { redirect_to attachments_path, notice: 'Attachment was successfully created.' }
end
但是,@note在create
代码发生时为零。
感谢您的帮助!
您无法访问在create中的新操作中初始化的实例变量。创建是一个单独的http请求,它不知道有关实例变量的任何信息@note – usha
是否有某种方法将@note值存储在全局变量中并在create中使用? – Reddirt