开发者

How to change text field on select drop down box?

开发者 https://www.devze.com 2023-02-28 15:44 出处:网络
<p> <%= f.label :website_name %><br /> <%= f.text_field :website_name %> </p>
<p>
    <%= f.label :website_name %><br />
    <%= f.text_field :website_name %>
  </p>
  <p>
    <%= f.label :website_type %><br /开发者_JAVA技巧>
    <%= f.select :website_type, Media::ALL_MEDIA_TYPES, :include_blank => true %>
  </p>

  <p>
    <%= f.label :media_link %><br />
    <%= f.text_field :media_link %>
</p>

Here I want to set the pattern in "media_link" on selecting option in drop down. If I choose facebook in drop down then media_link should accept only facebook link pattern. for example http://www.facebook.com/stevemaddon. Option in drop down can be blogs or twitter.

Thanks,


I would do something like:

validate :social_media_format

MEDIA_LINK_FORMATS = {
  :twitter => /^http:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)$/,
  :facebook => /.../
}

def social_media_format
  errors.add(:media_link, "does not match #{website_type}") unless media_link =~ MEDIA_LINK_FORMATS[website_type.to_sym]
end


I'm gonna be real with you, I'm not sure if you're trying to do this dynamically or what, but take this and tell me how this does/doesn't work for you.

input = [:media_link]
if params[:website_type] == #Facebook website_type id
  if !input["facebook.com/"] #Or whatever regexp you want to use
    redirect_to :back, flash[:message] => "Must be a Facebook URL"
  end
end
if params[:website_type] == #Twitter website_type id
  if !input[/^http:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)$/] 
    redirect_to :back, flash[:message] => "Must be a Twitter URL"
  end
end
0

精彩评论

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

关注公众号