Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4在视图或模板中传递变量_Ruby On Rails_Variables_Ruby On Rails 4_Views - Fatal编程技术网

Ruby on rails Rails 4在视图或模板中传递变量

Ruby on rails Rails 4在视图或模板中传递变量,ruby-on-rails,variables,ruby-on-rails-4,views,Ruby On Rails,Variables,Ruby On Rails 4,Views,我有一个应用程序,允许所有者(但不是公共用户)上传相册文件和照片。在对视图进行编码时,我注意到一些奇怪的情况。在我的albums/index.html.erb文件中的do块中,如果传入变量@album.id,我会得到NilClass的NoMethodError。但是,如果删除“@”(或完全删除该变量),它就可以正常工作 但是在我的albums/show.html.erb文件中,在用于编辑专辑标题的代码行的link_中,我需要传递“@album.id”(或完全忽略变量)才能使其工作 为什么呢 这是

我有一个应用程序,允许所有者(但不是公共用户)上传相册文件和照片。在对视图进行编码时,我注意到一些奇怪的情况。在我的albums/index.html.erb文件中的do块中,如果传入变量@album.id,我会得到NilClass的NoMethodError。但是,如果删除“@”(或完全删除该变量),它就可以正常工作

但是在我的albums/show.html.erb文件中,在用于编辑专辑标题的代码行的link_中,我需要传递“@album.id”(或完全忽略变量)才能使其工作

为什么呢

这是我的相册/index.html.erb文件和代码

    <div class="admin_login"><%= link_to "Admin Login", new_album_path %></div>
    <div class="home"><%= link_to "Home", root_path %></div>

    <h1>Albums Gallery</h1>

    <% @albums.each do |album| %>
      <div>
        <%= link_to album.name, album_path(album.id) %>    
      </div>
    <% end %>

唱片库
这是我的albums/show.html.erb文件:

<h3><%= link_to @album.name %></h3>
<div class="album">
  <%= link_to "Edit", edit_album_path(@album.id) %>
  <%= link_to "Delete", @album, method: :delete, data:{confirm: "Are you sure you want to    delete this album? All photos in it will be permanently deleted!"} %>
</div>
<br><%= link_to "Back", albums_path %>


为清楚起见,以下是我的相册控制器代码:

    class AlbumsController < ApplicationController
      def index
        @albums = Album.all
      end

      def new
        @album = Album.new
      end

      def create
        @album = Album.new(album_params)
        @album.save
        redirect_to albums_path
      end

      def show
        @album = Album.find(params[:id])
      end

      def edit
        @album = Album.find(params[:id])    
      end

      def update
        @album = Album.find(params[:id])
          if @album.update(album_params)
            redirect_to album_path(@album.id)
          else
           render 'edit'
          end
      end

      def destroy
        @album = Album.find(params[:id])
        @album.destroy
        redirect_to albums_path
      end

      private

      def album_params
        params.require(:album).permit(:id, :name, :category)
      end
    end
class AlbumsController
您需要在控制器中设置实例变量。确保在AlbumsController索引操作中设置@albums,并在show操作中设置@album。do块使用块变量,而不是实例,因此不需要@。

在索引操作中,您将一系列相册定义为
@albums
。在show action中,您只定义了一个
@album
。这些变量只能在定义它们的操作中访问

“album”在索引视图中起作用的原因是,每个块都在块的范围内定义一个本地“album”变量

<% @albums.each do |album| %>
  <div>
    <%= link_to album.name, album_path(album.id) %>    
  </div>
<% end %>


do块后的
| album |
表示“对于此迭代,将当前值分配给变量
album

注意:如果我在index.html.erb文件的do块中完全省略了(album.id),并且如果我在链接u中也完全省略了(@album.id)以进行“编辑”,则应用程序(到目前为止)可以工作我的show.html.erb文件中的代码。这激发了我的好奇心,我已经做到了。如果我不这样做,它可能根本不起作用。忽略我答案的第一部分,我发现你的代码没有问题。我有点明白,但我仍然不明白,如果我从
代码行中删除(album.id)并删除(@album.id),应用程序为什么会工作分别从代码的
行。我的意思是,完全删除这些变量后,应用程序仍然可以工作。我不明白为什么应该这样做。或者这是因为Rails的多模态特性?