Topic: Get form value - RJS

Hello,

How to get value from form input field, using RJS? I am able to pass this value to controller using :with statement, but I need to interpret form value without sending to controller. I need something like:

page[:fieldid][:value]='new_value'

but working reverse way.

thanks in advance for help,
Animus

Re: Get form value - RJS

You can access the "params" hash in an RJS template like you can in the controller and other views. Does that work for you? If not, check the exact parameters that are being sent in the development.log file and make sure they match.

Railscasts - Free Ruby on Rails Screencasts

Re: Get form value - RJS

will accessing to params hash require form submitting?
I will precise my problem:

I have rhtml file with inline rjs, sth like:

<%= link_to_function "update" do |page|
  #here I need to have input form text value in ruby variable
end%>

<form>
<input id="navi" type="input" name="mytextfield" value="somevalue"/>
</form>


I do not want to invoke controller, because action I want to perform is pure javascript (highlights on page) but also I need to collect some of values that appeared in text field.

Last edited by Animus (2007-05-10 15:43:22)

Re: Get form value - RJS

Oh I see, this is all handled on the client side so there is no server-side request happening. I think this returns the value:

page[:fieldid].value

Actually, this doesn't return the value, it returns the javascript which when executed returns the value. It's important to know that RJS just generates javascript, ruby can't actually tell what's happening on the client side because the ruby gets executed on the server. If you want to do anything client side has to be all handled in Javascript. RJS can help by generating the javascript, but that's it.

Does that make sense?

Railscasts - Free Ruby on Rails Screencasts

Re: Get form value - RJS

Excuse me if I don;t know something obvious!
Is it possible to pass value return from js to controller?, something like:
View:

<%= link_to_remote "add", :update => "langauges_div", :url => {:action => :add_language, :lang => page[:languages].value } %>

<select id="langauges"><option value="Eng">English</option><option value="Kor">Korean</option></select>


Controller:
...
def add_language
  and use "lang" here
end

Thanks

Last edited by ahoge (2007-05-16 19:24:42)

iChat ahoge_@mac.com

Re: Get form value - RJS

You can do this with the "with" parameter. I'm a little unsure of the exact syntax, but I think this will work:

<%= link_to_remote "add", ..., :with => "'lang=' + $('languages').value" %>

Then you should be able to access it with params[:lang] in the resulting action.

Railscasts - Free Ruby on Rails Screencasts

Re: Get form value - RJS

ryanb wrote:

You can do this with the "with" parameter. I'm a little unsure of the exact syntax, but I think this will work:

<%= link_to_remote "add", ..., :with => "'lang=' + $('languages').value" %>

Then you should be able to access it with params[:lang] in the resulting action.

Thx ryanb !
Btw, I like Your podcasts!

There is also way to do get form values without submitting it:
http://prototypejs.org/api/form/serialize

Last edited by ahoge (2007-05-18 10:10:57)

iChat ahoge_@mac.com