Tag: partials

为什么控制器方法中定义的实例变量不在相应的部分中可用?

例如,给定一个基本控制器,例如: class PostsController < ApplicationController def index @post = Post.all end 实例变量@post在视图posts/index.html.erb可用,但不在部分posts/_index.html.erb 我的主要问题是:这是为什么? 如何在控制器中为部分定义方法,例如,传递实例变量? 更具体的说明:我问这个是因为我在单独的主视图welcome/index.html.erb呈现数据的方式是通过这个post部分。 我已经通过posts/_index.html.erb partial中的Post.all直接调用模型来解决这个限制,但据我所知,这忽略了MVC架构并严重限制我开发更复杂的方法,除非我真的打破约定并将它们写入视图(我认为它可能有用,但设计很差)。 目前,@ post将nil传递给部分符号:welcome / index.html.erb中的post 很明显我错过了什么,所以任何见解都会非常感激! 谢谢 相关文件: views/ posts/ _index.html.erb _new.html.erb show.html.erb welcome/ index.html.erb controllers/ posts_controller.rb welcome_controller.rb 文章/ _index.html.erb 文章/ _new.html.erb 文章/ show.html.erb welcome / index.html.erb相关部分(部分) 控制器/ posts_controller.rb class PostsController < ApplicationController def new end def create @post […]

将实例变量传递给js.erb文件(Rails 3 / jQuery)

我有一个“index.html.erb”文件,其中包含以下内容: 这将呈现“_user.html.erb”并输出一个按钮,用于对每个用户执行特定操作: “#{user.id} _action”,:remote => true)%> 我已经设置了我的用户控制器,通过查看“action.js.erb”来响应AJAX请求。 为了对partial中的特定用户执行javascript方法,我想知道如何将我的partial(例如user.id)中的实例变量传递到js.erb文件中或在js.erb文件中访问,例如: $(“#{@user.id}_action”).toggle();

在不同的控制器中渲染表单部分(不嵌套)

我有两个生成脚手架的模型,一个是LogBook,另一个是LogEntry。 我想在LogBook显示页面上为LogEntry渲染部分表单。 当我在局部调用渲染时,我收到此错误: undefined method `model_name’ for NilClass:Class 我假设这是因为默认_form使用的实例变量在从单独的控制器调用时不存在。 所以我尝试将LogEntry _form.html.erb转换为使用局部变量并通过render调用传递它们。 在此之后是错误: Model LogEntry does not respond to Text 如何将此部分包含在显示页面中以形成不同的控制器? 楷模: class LogBook :destroy end class LogEntry “log_book”, :foreign_key => “log_book_id” end LogEntry _form.html.erb(使用局部变量): prohibited this log_entry from being saved: LogBook show.html.erb: Name: @log_book.LogEntries.new %> |

将局部变量传递给Rails 4中用于Ransack关联的部分层次结构

我正在尝试使用Ransack在我的所有模型的索引页面上实现搜索和排序表单。 我想尽可能干这样做,所以我在views / application文件夹中创建了三个部分: 视图/应用/ _table_search_and_sort_form.html.erb 视图/应用/ _condition_fields.html.erb 视图/应用/ _sort_fields.html.erb 每个代码如下: 视图/应用/ _table_search_and_sort_form.html.erb 视图/应用/ _condition_fields.html.erb <div class="field" data-object-name="”> 视图/应用/ _sort_fields.html.erb <div class="field" data-object-name="”> 在我的每个控制器中,我都有类似的东西: def index @search = Document.search(params[:q]) @documents = @search.result.paginate(:page => params[:page]) @search.build_condition if @search.conditions.empty? @search.build_sort if @search.sorts.empty? end 然后我通过添加以下内容将表单添加到每个index.html.erb: 其中:associations数组对于每个模型是不同的,这取决于我希望可以从条件表单访问的关联。 当我不尝试通过两个部分向下传递:associations数组时,只需将数组硬编码到_condition_fields.html.erb中,例如: 一切正常(对于那个模型,在这种情况下为Document)。 当我尝试通过将:associations数组从索引传递到_table_search_and_sort_form并将其传递到_conditional_form来将其扩展到多个模型时,我收到以下错误: undefined local variable or method `associations’ Extracted source (around […]

在Valuations#new中的ActionController :: UrlGenerationError

我读过与UrlGenerationError有关的其他SO文章, UrlGenerationError文章似乎指向一个单词的单一化或多元化,但我认为这不是问题所在。 当我从valuations / _form.html.erb中删除时,它可以正常工作: 使用:name & :tag_list提交:tag_list ,readd 然后刷新。 什么是零交易? 路线 resources :valuations do resources :comments end comments_controller class CommentsController < ApplicationController before_action :load_commentable before_action :set_comment, only: [:show, :edit, :update, :destroy] before_action :logged_in_user, only: [:create, :destroy] def index @comments = @commentable.comments end def new @comment = @commentable.comments.new end def create @comment = @commentable.comments.new(comment_params) if […]

Rails从application.html.erb渲染实例变量

我正在使用Rails 4书来关注敏捷Web开发,我对渲染方面有点困惑。 该问题的简单版本是…在application.html.erb文件中 render @cart 这很令人困惑,因为我认为需要有一个与该视图相关联的控制器才能知道要使用哪个部分和@cart变量。 是否只是通过命名约定,该行寻找像_cart.html.erb这样的部分? 在那种情况下,它实际上不知道@cart是什么,直到它呈现部分? 一些澄清是可爱的。 谢谢!

如何在非rails应用程序中实现erb partials?

我想做这样的事情: require ‘erb’ @var = ‘test’ template = ERB.new File.new(“template.erb”).read rendered = template.result(binding()) 但是如何在template.erb中使用partials?