开发者

How to write a method in a ruby controller and then to the ":value =>" of a text_field

开发者 https://www.devze.com 2023-03-27 23:04 出处:网络
Im very new to programming with ruby on rails. I need to write a method for changed default values in a text_field.

Im very new to programming with ruby on rails.

I need to write a method for changed default values in a text_field. It should look something like this:

count = 0
def value()
{
  count++
  value = ""
  if count == 1
    value = "Pre-Sales"
  elsif count == 2
    value = "Project"
  etc...
  end
  return value
}
end

this should mean that everytime the method is run then the count goes up by 1. im calling the method here:

<p class="fields">
    <%= f.label :task_name, "Task Name" %>
    <%= f.text_field :task_name, :value => value %>
    <%= link_to_remove_fields "remove", f %>
</p>

its inside a partial too... I know im missing alot he开发者_JS百科re and need someone to help.


I got a feeling that you're doing something crazy / "not Rails way".

Ok, so you enter a page and your form is rendered. I guess you're using form_for helper and a :task_name is a field in db related to some model right? What's the reasoning behind having :

 <%= f.text_field :task_name, :value => translated_1 %>
 <%= f.text_field :task_name, :value => translated_2 %>
 <%= f.text_field :task_name, :value => translated_3 %>
 <%= f.text_field :task_name, :value => translated_4 %>
 <%= f.text_field :task_name, :value => translated_5 %>

?

I don't understand it but let's say you have your paragraph in a partial. You could do:

5.times do |i|
  render :partial => "this_is_crazy", :locals => {:i => i, :f => f}
end

and then inside this partial you would have your:

<p class="fields">
    <%= f.label :task_name, "Task Name" %>
    <%= f.text_field :task_name, :value => get_default_value(i) %>
    <%= link_to_remove_fields "remove", f %>
</p>

then you would define your get_default_value(i) in "helpers/crazy_helper.rb" using a case or bunch of ifs... (that's a possibility, not a suggestion or the way I would do it)

But again, I got a feeling that you're doing something wrong... Maybe I you post something more about your problem, I will be able to get you back on the right track :)

0

精彩评论

暂无评论...
验证码 换一张
取 消