为什么我的变量是Nil? - Ruby on Rails

时间:2017-06-06 20:57:14

标签: html ruby-on-rails ruby

我有一个名为Produto的控制器和一个名为Produto的模型。我想将所有Produto对象传递给变量,就像Produto控制器一样,但是传递给另一个控制器。因此,控制器Produto在她的Index def中使用@Produto = Produto.all,并且该控制器的视图使用该变量。但我想在其他控制器中这样做。 看看我有什么:

产品控制器:

df = pd.DataFrame([["one", 1.2, "", "4,3"],
   ["two","1,7", "2,4", 0.55],
   ["three","", 5.4, "3,9"],
   ["comma, here","2,1",1.2,""]], 
   columns=['a','b','c','d'])

df = df.set_index('a')
df2 = df.apply(lambda x: x.str.replace(',','.')).combine_first(df).apply(lambda x:pd.to_numeric(x,errors='coerce')).reset_index()

print(df2)

             a    b    c     d
0          one  1.2  NaN  4.30
1          two  1.7  2.4  0.55
2        three  NaN  5.4  3.90
3  comma, here  2.1  1.2   NaN

家庭控制器(这是控制器,我也想采用那些Produto对象,就像Produto控制器一样):

class ProdutosController < ApplicationController
  before_action :set_produto, only: [:show, :edit, :update, :destroy]

  # GET /produtos
  # GET /produtos.json
  def index
    @produtos = Produto.all
  end

  def show
  end

  def new
    @produto = Produto.new
  end

  def edit
  end

  def create
    @produto = Produto.new(produto_params)
    respond_to do |format|
      if @produto.save
        format.html { redirect_to @produto, notice: 'Produto was successfully created.' }
        format.json { render :show, status: :created, location: @produto }
      else
        format.html { render :new }
        format.json { render json: @produto.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @produto.update(produto_params)
        format.html { redirect_to @produto, notice: 'Produto was successfully updated.' }
        format.json { render :show, status: :ok, location: @produto }
      else
        format.html { render :edit }
        format.json { render json: @produto.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @produto.destroy
    respond_to do |format|
      format.html { redirect_to produtos_url, notice: 'Produto was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_produto
      @produto = Produto.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def produto_params
      params.require(:produto).permit(:nome, :preco, :fornecedor_id)
    end
end

Home的观点在代码中有这部分:

    class HomeController < ApplicationController

  def index
    @produtos = Produto.all
    render "index"
  end

  def show
    if session[:user]
      @user = User.find(session[:user])
    end
  end

end

我收到此错误

  

未定义的方法`每个'为nil:&lt;%@ produtos.each中的NilClass   | produto | %GT;

我的路线:

  <table>
    <thead>
    <tr>
      <th>Nome</th>
      <th>Preco</th>
      <th>Fornecedor</th>
      <th colspan="3"></th>
    </tr>
    </thead>

    <tbody>
    <% @produtos.each do |produto| %>
        <tr>
          <td><%= produto.nome %></td>
          <td><%= produto.preco %></td>
          <td><%= produto.fornecedor %></td>
          <td><%= link_to 'Show', produto %></td>
          <td><%= link_to 'Edit', edit_produto_path(produto) %></td>
          <td><%= link_to 'Destroy', produto, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        </tr>
    <% end %>
    </tbody>
  </table>

登录控制器(这是根路由):

Rails.application.routes.draw do

  root 'login#new'
  get 'home#index'

  scope '/login' do
    get '/acesso', to:'login#new'
    post '/acessorecebendo', to:'login#create'
    get '/sair', to:'login#destroy'
  end

  resources :login
  resources :home
  resources :produtos
  resources :fornecedors
end

1 个答案:

答案 0 :(得分:0)

让您的路线,方法和观点匹配:

# config/routes.rb
get '/home/inicio', to: 'home#index'

# app/controllers/home_controller.rb
def index
  @produtos = Produto.all
end

# app/views/home/index.html.erb
<% @produtos.each do |produto| %>

如您所见,它仅更改URI,但您必须在控制器方法和视图名称中引用您的index方法。

如果您使用render 'inicio',则必须使用inicio.html.erb视图而不是index.html.erb