为什么在控制器的方法中定义的实例变量在相应的部分中不可用? [英] Why aren't instance variables defined in a controller's methods available in the corresponding partials?

查看:54
本文介绍了为什么在控制器的方法中定义的实例变量在相应的部分中不可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,给定一个基本的控制器,例如:

class PostsController <应用控制器定义索引@post = Post.all结尾

实例变量 @post 在视图 posts/index.html.erb 中可用,但在部分 posts/_index.html.erb 中不可用代码>

我的主要问题是:这是为什么?以及如何为控制器中的部分定义方法,例如传递实例变量?

更具体的说明:我问这个是因为我在一个单独的主视图中呈现数据的方式 welcome/index.html.erb 是通过这个帖子部分的.我通过在 posts/_index.html.erb 部分中使用 Post.all 直接调用模型来绕过这个限制,但据我所知,这忽略了 MVC 架构并且严重限制了我开发更复杂的方法,除非我真的打破惯例并将它们写入视图(我认为这可能有效但设计不佳).

目前,@post 正在向部分符号 :post 传递一个 nil 到welcome/index.html.erb

显然我遗漏了一些东西,所以任何见解将不胜感激!

谢谢

相关文件:

views/帖子/_index.html.erb_new.html.erb显示.html.erb欢迎/index.html.erb控制器/post_controller.rbWelcome_controller.rb

posts/_index.html.erb

<% post.reverse.each do |post|%><div class="panel panel-default"><div class="panel-body"><%= post.text %>

<%结束%>

posts/_new.html.erb

<%= form_for :post, url: posts_path do |f|%><p id="post_box"><%= f.text_area :text %></p><p><%= f.submit %></p><%结束%>

posts/show.html.erb

<%= @post.text %>

welcome/index.html.erb 相关部分(部分)

<%= render partial: "posts/index", locals: {post: @post} %><%= 渲染部分:"posts/new" %>

controllers/posts_controller.rb

class PostsController <应用控制器定义新结尾定义创建@post = Post.new(post_params)@post.save重定向到/"结尾高清秀@post = Post.find(params[:id])结尾定义索引@post = Post.all结尾私人的def post_paramsparams.require(:post).permit(:text)结尾结尾

控制器/welcome_controller.rb

class WelcomeController <应用控制器结尾

解决方案

您是对的,您在视图中调用控制器级对象查找违反了 MVC 约定.

当您对部分调用渲染操作时,您需要发送各种参数,其中之一是 locals 哈希,它采用您想直接传递到部分的变量.

看起来像这样:

<%= render partial: "something", locals: {posts: @posts} %>

Rails 指南是有关该主题的重要资源.在此处了解更多相关信息.

For example, given a basic controller such as:

class PostsController < ApplicationController
  def index
    @post = Post.all
  end

The instance variable @post is available in the view posts/index.html.erb but not the partial posts/_index.html.erb

My primary questions are: Why is this? and How can I define a method for a partial in a controller to for example, pass an instance variable?

On a more specific note: I ask this because the way I am rendering data in a separate, primary view welcome/index.html.erb is via this posts partial. I have gotten around this limitation by directly calling the model with Post.all in the posts/_index.html.erb partial, but as far as I understand this ignores MVC architecture and severely limits me from developing more complex methods, unless I really break convention and write them into the view (which I assume may work but is poor design).

Currently, @post is passing a nil to the partial symbol :post in welcome/index.html.erb

Clearly I'm missing something, so any insight would be greatly appreciated!

Thank you

Relevant files:

views/
  posts/
    _index.html.erb
    _new.html.erb
    show.html.erb
  welcome/
    index.html.erb

controllers/ 
  posts_controller.rb
  welcome_controller.rb

posts/_index.html.erb

<% post.reverse.each do |post| %>
  <div class="panel panel-default">
    <div class="panel-body">
      <%= post.text %>
    </div>
  </div>
<% end %>

posts/_new.html.erb

<%= form_for :post, url: posts_path do |f| %>
  <p id="post_box">
    <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

posts/show.html.erb

<%= @post.text %>

welcome/index.html.erb relevant portions (partials)

<%= render partial: "posts/index", locals: {post: @post} %>
<%= render partial: "posts/new" %>

controllers/posts_controller.rb

class PostsController < ApplicationController
  def new
  end

  def create
    @post = Post.new(post_params)

    @post.save
    redirect_to '/'
  end

  def show
    @post = Post.find(params[:id])
  end

  def index
    @post = Post.all
  end

  private
    def post_params
      params.require(:post).permit(:text)
    end

end

controllers/welcome_controller.rb

class WelcomeController < ApplicationController
end

解决方案

You're right that you're breaking MVC conventions by calling controller-level object lookups in your view.

When you call the render action on the partial you have a variety of arguments to send, one of which is a locals hash that takes a variable you want to pass directly into your partial.

It looks like this:

<%= render partial: "something", locals: {posts: @posts} %>

The rails guides are a great resource on the topic. More on that here.

这篇关于为什么在控制器的方法中定义的实例变量在相应的部分中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆