RoR - redirect_to undefined?

时间:2017-12-03 16:09:17

标签: ruby-on-rails

目前正在进行教程并遇到重定向问题。

Rails打印出以下消息:

undefined method ` redirect_to' for #<PortfoliosController:0x00007fc6ec31b108> Did you mean? redirect_to

PortfolioController:

class PortfoliosController < ApplicationController

  def index
    @portfolio_items = Portfolio.all
  end

  def new
    @portoflio_item = Portfolio.new
  end

  def create
    @portfolio_item = Portfolio.new(params.require(:portfolio).permit(:title, :subtitle, :body))

    respond_to do |format|
      if @portfolio_item.save
        format.html { redirect_to portfolios_path, notice: 'Portfolio was successfully created.' }

      else
        format.html { render :index }
      end
    end
  end

  def edit
    @portfolio_item = Portfolio.find(params[:id])
  end

  def update
    @portfolio_item = Portfolio.find(params[:id])

    respond_to do |format|
      if @portfolio_item.update(params.require(:portfolio).permit(:title, :subtitle, :body))
        format.html { redirect_to portfolios_path, notice: 'Portfolio item successfully updated!'}
      else
        format.html { render :edit }
      end
    end
  end








end

正如你所看到的,我在def create中也使用了redirect_to而没有任何问题,但def update说redirect_to是未定义的?

有什么想法吗?

提前致谢!

1 个答案:

答案 0 :(得分:2)

当你输入 redirect_to时,这几乎是正确的,并且Rails告诉你可能你的意思是redirect_to,那么问题就像在大多数情况下,当打开花括号时,意外增加了一个不间断的空间。除非您使用的是处理不可见字符的编辑器,否则您可以意识到这一点,否则错误将存在,直到Rails抛出错误。

只需删除{respond_to之间的空格。

对于Atom,请参阅atom-highlight-bad-chars 对于Sublime,请参阅Dealing with Alt+Space (non-breakable space) in Sublime Text