Problems with params[:commit]

Hi,
Can someone tell me why doesnt this work? I have two links the action of
both links are the same. They post the data from the form and suppose to
redirect to two different pages respectively if i click any one of those
links. The error I got is TEMPLATE IS MISSING. I didnt create that
template because this method is suppose to redirect to another page.

if @user.save
          if params[:commit] == "Previous"
            redirect to some page

          elsif params[:commit] == "Next"
                redirect to some page
          end
      else
        render the page
    end

The problem is that redirect does not return, so it is falling through the bottom of the method. Try this:

if @user.save
          if params[:commit] == "Previous"
            return redirect to some page

          elsif params[:commit] == "Next"
            return redirect to some page
          end
      else
        render the page
    end

-Bill

user splash wrote:

William Pratt wrote:

The problem is that redirect does not return, so it is falling through
the bottom of the method. Try this:

if @user.save
          if params[:commit] == "Previous"
            return redirect to some page

          elsif params[:commit] == "Next"
            return redirect to some page
          end
      else
        render the page
    end

-Bill
Sincerely,

William Pratt

Hi,
I've tried the return redirect to some page but the same TEMLPATE
MISSING error is being shown.

This is what I did:

        if user.save
            if params[:commit] == 'Previous
              return redirect_to :action => 'previous_page'

            elsif params[:commit] == 'Next'
                return redirect_to :action => next_page'
            end
        else
          render :action=> 'current_page'
      end

Please correct me if I am wrong

Thanks

Show us your form code please.

I thought redirect_to DID do a return, in fact I’m sure of it. Try putting a redirect_to(:action => “index”) and then a puts directly after it.

Can you show us your form code and the exact error please?

Ryan Bigg wrote:

Show us your form code please.

           redirect to some page
>

Hi,
This is my form,

<% form_for :user, :url => { :action => :save_user}, :html => { :id =>
"user" } do |n| %>
<label> Username:</label>
<%= n.text_field :username %>

<label> Password:</label>
<%= n.text_field :password %>

<%= link_to_function 'Previous', "$('user').submit()" -%>
<%= link_to_function 'Next', "$('user').submit()" -%>

Both links save user. And are suppose to redirect the user to
previous_page and next_page respectively. I understand button is a
better and a more "right" way. But is there a easier way to post using
links?

Thanks

Instead of the <%= link_to_function %>, use

<%= submit_tag “Previous” %>
<%= submit_tag “Next” %>

Ryan Bigg wrote:

Instead of the <%= link_to_function %>, use

<%= submit_tag "Previous" %>
<%= submit_tag "Next" %>

--
Posted via http://www.ruby-forum.com/.

>

--
Ryan Bigg
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

Hi,
Thanks. I have tried the above submit_tag and it works. But can anyone
tell me if there is a way to make link work like this submit_tag ?

Thanks

user splash wrote:

Ryan Bigg wrote:

Instead of the <%= link_to_function %>, use

<%= submit_tag "Previous" %>
<%= submit_tag "Next" %>

--
Ryan Bigg
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

Hi,
Thanks. I have tried the above submit_tag and it works. But can anyone
tell me if there is a way to make link work like this submit_tag ?

Thanks

Hi,
Say if i create a link using:

<%= link_to 'PREVIOUS', {:controller => 'create_user,
       :action => "save_user", :method => :post} %>

It does a post but it doesnt get the vaules from the fields and post
them. It posts empty fields.

Can someone help me to resolve this?

Thanks

Use CSS to style the submit_tag how you want it.

I use a method similiar to your first approach: I have a javascript function which submits the form via AJAX. You can do it various ways, but here is the most straight-forward if you don't need to do anything with the form values before sending them to the server:

function submit_task()
{
  clear_messages();
  
  var f = $('submit_task_form');
  var ok = form_check_required_fields(f);
  if (!ok)
  {
    alert('Required information missing.');
    return false;
  }
  else
  {
    form_strip(f);
    new Ajax.Request('/wgg/submit_task', {
      method: 'post',
      parameters: f.serialize(),
      onFailure: function(){ alert('Unable to submit task.') },
      onException: function(o,e) { alert(e) }
    });
  }
}

In this function, I have some utility methods that I use (clear_messages, form_check_required_fields, and form_strip), so don't get tripped up by those. The most important part for your case is the new Ajax.Request block. Notice that the method is post and the parameters are f.serialize, so all of the form fields will be serialized and passed to the server just like if you used the submit_tag.

Depending on how you link to this function, you might also need to "return false" so the form is not submitted twice.

Peace,
Phillip

function submit_task()
{
  clear_messages();

  var f = $('submit_task_form');
  var ok = form_check_required_fields(f);
  if (!ok)
  {
    alert('Required information missing.');
    return false;
  }
  else
  {
    form_strip(f);
    new Ajax.Request('/wgg/submit_task', {
      method: 'post',
      parameters: f.serialize(),
      onFailure: function(){ alert('Unable to submit task.') },
      onException: function(o,e) { alert(e) }
    });
  }
}

Hi,
Where do I put this function? In rhtml? I tried that and it show Unknown
action
No action responded to submit_task() error. I place the whole block of
code in <script></script> and call the function like this, <%= link_to
'PREVIOUS', onclick="submit_task()" %> Did I do something wrong? Please
explain.

Thanks

This is a javascript function that would go in a js file in public/javascripts. For testing purposes, you could put it in application.js, but I wouldn't leave it there long term. To call it, you'd do something like what you have

<%= link_to_function 'Previous', 'submit_task();' %>

See if that gets you any further.

Peace,
Phillip

Phillip Koebbe wrote:

This is a javascript function that would go in a js file in public/
javascripts. For testing purposes, you could put it in
application.js, but I wouldn't leave it there long term. To call it,
you'd do something like what you have

<%= link_to_function 'Previous', 'submit_task();' %>

See if that gets you any further.

Peace,
Phillip

Hi,
I've tried the codes. But I didn't get what I expected, to submit the
form and get redirected to the next/previous page. Where did I go wrong
or maybe I missed some steps or maybe I did not totally understand the
function 'submit_task'. Please explain.

function submit_task()
{
   clear_messages();

   var f = $('submit_task_form'); ///// WHAT DOES THIS DO? DO I HAVE TO
CHANGE THIS?

  var ok = form_check_required_fields(f);
   if (!ok)
   {
     alert('Required information missing.');
     return false;
   }
   else
   {
     form_strip(f);
     new Ajax.Request('/wgg/submit_task', { //////WHAT DOES THIS DO? DO
I HAVE TO CHANGE THIS?

       method: 'post',
       parameters: f.serialize(),
       onFailure: function(){ alert('Unable to submit task.') },
       onException: function(o,e) { alert(e) }
     });
   return false; ////////IS IT RIGHT TO RETURN FALSE HERE TO PREVENT THE
FORM FROM BEING SUBMITED TWICE?
    }
}

Thanks

Hm. Please don't take offense to this, but you might be in over your head right now. I posted that function as an example of what you *can* do. I didn't intend for you to try to use it verbatim in your code. You were having a problem getting all of your form fields to make it across to your controller. This method was shown to give you an example of how to serialize a form.

But to answer some of your questions...

var f = $('submit_task_form'); assigns the form to the local variable f. $() is a Prototype function. f will be passed to other functions to do things that I need to do. In my original message, I mentioned that there were a few functions that you didn't need to pay attention to.

/wgg/submit_task is an action in a controller in my application. You would use whatever action you post your form to.

the "return false" at the end of the function is *not* sufficient to prevent double submission of the form. You would need to include "return false" in the event handler that calls the javascript function:

onclick="some_function(); return false;"

Let me ask you a question: have you spent much time working through any tutorials? How about the Agile Web Development with Rails book? I don't mean to criticize you, especially for being new to RoR, but it seems to me like you have a lack of understanding of the fundamentals of Javascript and RoR. Trying to take someone else's code, offered as an example, and just drop it into your application and hope it works is rarely going to get you the results you desire.

I am pretty new to RoR. It's been about 9 months now. I started with the above mentioned book and worked on a piddly little app, just to get my bearings. I've been working on some "real apps" now for a few months, and I'm learning more and more every day. While not technically mind-boggling, some of the things I'm doing are not going to make a whole lot of sense to someone who is not yet versed in the interaction between Javascript and RoR.

Please, for your own good, step back from this project and think about what you are trying to accomplish. Take a little time and work through some things, step by step, until you get a feel for how it all works. You'll save yourself a whole bunch of hair pulling. Really.

Peace,
Phillip