首页 \ 问答 \ 如何在rails中使用form_for helper?(How to use form_for helper in rails?)

如何在rails中使用form_for helper?(How to use form_for helper in rails?)

铁路新手。 我在rails 2.3.5中使用form_for helper。 问题是,在编辑记录时,我不能使用:

<% form_for :myrecord, :url => {:action => :update} %> ... <% end %>

但我必须使用:

<% form_for @myrecord, :url => {:action => :update} %> ... # the same as above <% end %>

当我使用第一个代码时,它总是返回错误说未定义的方法。 当“myrecord”是从控制器传递到视图的实例的名称时,第一个代码和第二个代码是否应该相同?

感谢您提供任何提示和答案。


New to rails. I am using form_for helper in rails 2.3.5. The question is, when editing a record, I cannot use:

<% form_for :myrecord, :url => {:action => :update} %> ... <% end %>

But I have to use:

<% form_for @myrecord, :url => {:action => :update} %> ... # the same as above <% end %>

When I use the first code, it always return error saying undefined method. Shouldn't the first code and the second code serves the same when "myrecord" is the name of the instance passed from controller to view?

Thank you for any hints and answers.


原文:https://stackoverflow.com/questions/2396055
更新时间:2023-07-08 13:07

最满意答案

您应该创建一个具有Instellingen属性的接口并在三个类中实现它。

例如:

interface IHasInstellingen {
    Instellingen Instellingen { get; }
}

public class Class1: Label, IHasInstellingen {
    public Instellingen Instellingen { get; private set; }
}
public class Class2: Button, IHasInstellingen {
    public Instellingen Instellingen { get; private set; }
}


private void GetInstellingenFromClass(IHasInstellingen c) {
    Instellingen ig = c.Instellingen;
    //Do things...
}
//Alternatively:
private void GetInstellingenFromClass(Control c) {
    IHasInstellingen hi = c as IHasInstellingen;
    if (hi == null)
        return;     //Or throw an ArgumentException

    Instellingen ig = hi.Instellingen;
    //Do things...
}

You should make an interface that has an Instellingen property and implement it in the three classes.

For example:

interface IHasInstellingen {
    Instellingen Instellingen { get; }
}

public class Class1: Label, IHasInstellingen {
    public Instellingen Instellingen { get; private set; }
}
public class Class2: Button, IHasInstellingen {
    public Instellingen Instellingen { get; private set; }
}


private void GetInstellingenFromClass(IHasInstellingen c) {
    Instellingen ig = c.Instellingen;
    //Do things...
}
//Alternatively:
private void GetInstellingenFromClass(Control c) {
    IHasInstellingen hi = c as IHasInstellingen;
    if (hi == null)
        return;     //Or throw an ArgumentException

    Instellingen ig = hi.Instellingen;
    //Do things...
}

相关问答

更多
  • 在makeDS的定义中,变量t的类型为[Contain] makeDS [Contain] (即makeDS的列表),所以当你说t!!0这将提取该列表的第一个元素,其类型为Contain 。 问题是DataSubject的name字段包含一个String (它是[Char]的别名)。 因此,您试图在[Char]的位置存储Contain ,这是不可能的,因为类型不同。 您需要在代码中使用不同的方法。 一个问题是每个DataSubject值代表DataSubject的单个字段。 因此,如果我们获得Contain ...
  • 首先,这是重复的 铸造和转换有什么区别? 这是受到启发的 (type)objectname.var中的(type)是什么 我先读这些,然后再回到这个简短的总结。 我引用您的说明书第6章的第一段,其中指出: 转换使表达式可以被视为特定类型。 转换可能会导致给定类型的表达式被视为具有不同的类型,或者它可能导致没有类型的表达式获取类型。 转换可以是隐式或显式的,这决定了是否需要显式转换。 例如,从int类型到long类型的转换是隐式的,因此int类型的表达式可以隐式地被视为long类型。 从long类型到int类 ...
  • 使用out和ref参数时,变量类型必须与参数类型匹配 。 主要原因是要确保类型安全,看看这个例子有一个想法: public static void Do(out object value) { value = new Foo(); } string myStr; Do(out myStr); // OMG I'm setting Foo inside a string !! When you use out and ref parameter the variable type must ma ...
  • 这里的转换是一个无操作,因为ToInt32返回一个int而int和Int32是相同的东西 。 所以,是的,你可以而且应该删除那里的演员。 我的猜测是代码的原作者不太了解C#。 如果你要转换一个真的很long的Int64 ,那么你应该把它分配给long而不是整数。 如果您只关心它是否能够将它作为一个数字使用并且您没有返回它 - 我建议您只需将其分配给var并让类型推断为您处理它。 The cast here is a no-op, as ToInt32 returns an int anyway and in ...
  • 如果您匹配的时间值具有相同的数字位数,那么这只会起作用,因为它会进行强有力的比较。 更好的解决方法是: PREFIX xsd: ... FILTER(xsd:integer(?time) > 1291908000) 这会将时间的值转换为整数,然后对其进行数字比较。 The solution to this was merely to add quotes around the timestamp.
  • 问题不在列表结构中,它在这一行: select new Order() {ID = order.ID, OrderDate=order.OrderDate }; 问题是你不能在查询中显式创建一个实体。 当您尝试创建列表时,会发生这种情况,因为IEnumerable实际上并未枚举,直到您尝试将此代码包装在new List行中,因为延迟执行了查询。 它看起来像你试图自己检索订单。 答案可能只是选择订单,而不是尝试构建新订单: IEnumerable MyQuery = from o ...
  • 您应该创建一个具有Instellingen属性的接口并在三个类中实现它。 例如: interface IHasInstellingen { Instellingen Instellingen { get; } } public class Class1: Label, IHasInstellingen { public Instellingen Instellingen { get; private set; } } public class Class2: Button, IHasIns ...
  • 首先,以增量方式处理此问题。 也就是说,首先只需读取键盘并通过串行线路在PC上显示其输出。 在使用此工作之后,编写代码将条目转换为整数。 请提供仅显示键盘条目的代码示例,然后提供将该条目转换为整数的示例。 First of all, approach this problem in increments. That is, first simply read the keypad and display its output via the serial line back at the PC. After ...
  • 如果它的字符串来自/来自其他类型,则stringstream或boost :: lexical_cast 。 对于其他类型,它将取决于类型,但也许查找标准的演员模板? static_cast和dynamic_cast应该完成你需要的大部分事情,或者const_cast和reinterpret_cast往往只对处理遗留系统有用。 If it's string to/from other types then stringstream or boost::lexical_cast. For other type ...
  • 是的,这只是明确指出类型必须是CHA.ByteString 。 这确实(本身) 不会产生任何类型的转换,它只是编译器(和/或读者)的一个提示,即res必须具有这种类型。 这些类型的本地注释是当一个值是由多态结果函数产生的 ,并且只有具有多态参数的函数消耗时才需要的。 一个简单的例子: f :: Int -> Int f = fromEnum . toEnum 在这里, toEnum将整数转换为任意的可枚举类型 - 例如可以是Char 。 无论您选择什么类型, fromEnum都能够将其转换回来...... ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。