How do I create a function

Can someone explain to me how I create a function in ROR, where put it
and
how I access it? Do I just create another action? What is the correct
way to do this? Any help would be appreciated.

Jason

Jason Hhh wrote in post #961994:

Can someone explain to me how I create a function in ROR,

By learning Ruby.

where put it
and
how I access it? Do I just create another action? What is the correct
way to do this?

That depends on what you're trying to achieve.

Any help would be appreciated.

Help: go learn Ruby.

Jason

Best,

Marnen,

That was absolutely no help.

Thanks anyways.

Jason

Marnen,

That was absolutely no help.

Your question is very vague, and there is a lot of information available online… if you google ‘ruby function’ you will find this link, which gives an answer as broad as your question:

http://www.howtogeek.com/howto/programming/ruby/ruby-function-method-syntax/

Along with other links.

If you want more specifics please give them, we are not mind readers (most of us) - i.e. what are you doing… creating a new controller action? A function in a model? A helper? Did you create a Rails app? Do you have Rails installed? Do you have Ruby installed?

I have a rails app. I understand how to create a function in Rails.

I have a contact that is viewable by multiple users. I want to create a
function to lock and unlock the contact. I have a field in the contact
database called locked.

I assume I create a new function in the action controller or contact
controller like:

def lock(contact_id)
  c=Contact.find(contact_id)
  c.locked = 1
  c.save
end

def unlock(contact_id)
  c=Contact.find(contact_id)
  c.locked = 0
  c.save
end

Is this the proper way to do this? How do I make sure it's not
accessible through the through the URL: /Contacts/lock?

I have a rails app. I understand how to create a function in Rails.

I have a contact that is viewable by multiple users. I want to create a
function to lock and unlock the contact. I have a field in the contact
database called locked.

I assume I create a new function in the action controller or contact
controller like:

def lock(contact_id)
c=Contact.find(contact_id)
c.locked = 1
c.save
end

def unlock(contact_id)
c=Contact.find(contact_id)
c.locked = 0
c.save
end

Is this the proper way to do this? How do I make sure it's not
accessible through the through the URL: /Contacts/lock?

By keeping the function out of the controller. You may either place
lock and unlock in a helper file, or make them as a part of the model
itself. I, personally, vote for the latter. Rule of thumb: smart
models, thin controller.

Happy hacking!

Thanks Brian!

Just to hopefully elaborate on things a bit here to help you out Jason

Can someone explain to me how I create a function in ROR

I have a rails app. I understand how to create a function in Rails.

I'm a little confused as to what you need, as these two posts seem to
be contradictory.

I agree with the sentiment of Marnen's post; if you're confused at
this stage about how to structure your code in Ruby (hint, there's
very little difference in general terms with *any* other OO language -
differences in idiom, syntax and some functionality, sure - but if you
know Java or C#, you should be able to understand a Ruby class easy
enough. My point here is that your question possibly seems to be
partly about OO programming generally, not Rails specifically) or how
to organise the structure of a Rails application, then there are some
very good primers out there on the WWW, and several highly recommended
books on the subject too.

Is this the proper way to do this? How do I make sure it's not
accessible through the through the URL: /Contacts/lock?

I see you now already have a couple of suggestions, and I'll add the
"private" keyword to the mix as another alternative... they're all
choices, but I would prefer the approach of utilising methods on the
model.

PS Please try not to top-post your replies, but to interleave them
with appropriate quotes, to make following the conversation as easy as
possible for readers.

Regards

Thanks Phoenix Rising for the detailed explanation. This is just what I
needed. Much appreciated! I am understanding Rails more and more every
day.

Jason Hhh wrote:

I have a rails app. I understand how to create a function in Rails.

I have a contact that is viewable by multiple users. I want to create a function to lock and unlock the contact. I have a field in the contact database called locked.

I assume I create a new function in the action controller or contact controller like:

def lock(contact_id)
  c=Contact.find(contact_id)
  c.locked = 1
  c.save
end

def unlock(contact_id)
  c=Contact.find(contact_id)
  c.locked = 0
  c.save
end

Is this the proper way to do this? How do I make sure it's not accessible through the through the URL: /Contacts/lock?
  

You can create a 'private' section in your controller if you have something which belongs in the controller. The stuff in private is accessable from methods in the controller but not through the url.