简体   繁体   English

将实例变量从模型传递到视图

[英]Passing an instance variable from a model to a view

Hello (hopefully) omniscient Rails folks ;) 您好(希望如此)无所不知的Rails员工;)

I'm currently trying to generate a set of radio buttons from an instance variable in one of my models. 我目前正在尝试从一个模型中的实例变量生成一组单选按钮。 Because the MVC pattern prohibits passing data from models directly into the views, I somehow have to include the controller as kind of a middleman. 因为MVC模式禁止将数据直接从模型传递到视图中,所以我不得不以某种中间人的身份包括控制器。 The problem is that I can't access the instance variable of my model even though I use 'attr_reader :valid_statuses'. 问题是,即使我使用'attr_reader:valid_statuses',也无法访问模型的实例变量。 Therefore it's also not possible to pass the data down to my view... 因此,也无法将数据传递给我的视图...

This is how my user model looks like at the moment. 这就是我的用户模型目前的样子。 The '@valid_statuses' array contains the strings which should be used to generate the radio buttons. “ @valid_statuses”数组包含应用于生成单选按钮的字符串。

class User < ApplicationRecord
  attr_reader :valid_statuses
  @valid_statuses = %w(new editor admin blocked)
  validates :status, inclusion: { in: @valid_statuses }
end

The controller looks like this. 控制器看起来像这样。 @user is not nil, but @valid_statuses is @user不是nil,但是@valid_statuses是

class UsersController < ApplicationController
  ...
  def set_user
    @user = User.find(params[:id])
    @valid_statuses = @user.valid_statuses
  end
  ...
end

The _form.html.erb: _form.html.erb:

<div class="field">
  <%= f.label :status %>
  <% @valid_statuses.each do |item| %>
    <%= f.radio_button :status, item %> <%= item %><br />
  <% end %>
</div>

I would be very greateful if someone knows the answer to this and could share it with me and the rest of the world! 如果有人知道答案并可以与我和世界其他地区分享,我将非常感激! Thanks! 谢谢!

Valid statuses don't change from one User to another. 有效状态不会从一个用户更改为另一个用户。

So you define @valid_statuses inside the class User. 因此,您可以在User类中定义@valid_statuses @valid_statuses isn't defined for a specific @user , but for the whole class. @valid_statuses是不针对特定的定义@user ,而且对整个类。 attr_reader :valid_statuses tries to find the instance variable @valid_statuses of @user , which hasn't been defined. attr_reader :valid_statuses试图找到实例变量@valid_statuses@user ,尚未确定。

Here's a possible modification : 这是一个可能的修改:

class User < ApplicationRecord
  @valid_statuses = %w(new editor admin blocked)
  validates :status, inclusion: { in: @valid_statuses }
  def self.valid_statuses
    @valid_statuses
  end
end

and in your views : 并且在您看来:

User.valid_statuses

in this case, you have to use constants (if you wanna only fix your code): 在这种情况下,您必须使用常量(如果您只想修改代码):

model: 模型:

class User < ApplicationRecord
  VALID_STATUSES = %w(new editor admin blocked)
  validates :status, inclusion: { in: VALID_STATUSES }
end

controller: 控制器:

class UsersController < ApplicationController
  ...
  def set_user
    @user = User.find(params[:id])
  end
  ...
end

view: 视图:

<div class="field">
  <%= f.label :status %>
  <% User::VALID_STATUSES.each do |item| %>
    <%= f.radio_button :status, item %> <%= item %><br />
  <% end %>
</div>

BUT I better use enum with name status : 但是我最好使用名称status enum

model: 模型:

class User < ApplicationRecord
  enum status: [:new, :editor, :admin, :blocked]
end

READ NORE ABOUT ENUMs 阅读更多有关枚举的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM