0

I have _form.html.erb

<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :password %>
<%= f.password_field :password %>

Now if I render this form in homepage, HTML code should be:

<label for="session_name">Name</label>
<input id="session_name" name="session[name]" size="30" type="text">
...

If I change my _form.html.erb to:

<%= f.label :name %>
<%= f.text_field :name, disabled: true %>
...

HTML code should be:

...
<input disabled="disabled" id="session_name" name="session[name]" size="30" type="text">
...

But, I don't want to change my _form.html.erb, so how can I pass the disabled: true into my form? I tried to use render partial: but don't know syntax.

I just learn Ruby on Rails for 2 weeks, so please help me.

4

1 回答 1

0

我不知道你为什么不这样做,但你可以通过这种方法解决问题。尝试做-

在应用程序助手中,添加方法 -

def add_disabled
  #i suppose that u want the field to be disabled in the users controller for edit action but not for other controllers or actions
  return "disabled='disabled'" if params[:controller] == "users" and params[:action] == "edit"
end

在 _form.html.haml

= f.text_field :name, #{add_disabled}

这将调用辅助方法并根据控制器和操作返回“ disabled='disabled' ”

于 2012-10-09T07:25:53.937 回答