开发者

how to call a function on submit button?

i am new at rails .I have a mail controller and a mails table which i created through scaffold ,in my table i have a column current_status, i want that when i submit the form the status is set 开发者_运维百科according to submit button type, i have two buttons one is save and continue for which the status is "in_progress" and the other is "save in draft" for which the status is save as draft ,please help me , how i do this?


I'm not sure if you can specify which function (I'm assuming javascript function?) you want to call using rails' form generators. Rails pretty heavily associates one form, one button (and all rails forms seems to use the same autogenerated form functionality).

You might be stuck (if you do rails only) having a drop down menu or whatever deciding if it's a save or a continue.

Now, if you really want to have a conditional button like that, you probably wanna look at doing the form from scratch (using ajax, javascript, whatever) rather than letting rails build it for you. Rails is pretty neat when you want to do really basic stuff...but the whole point is convention over configuration, so when you want to get fancy it gets real stubborn.

Edit: You CAN specify which controller action you want a form to call, but it's still one controller per form. To have two different possibilities, you'd need to have two forms (which doesn't sound like what you're looking for?)


If you don't want to use JavaScript, you can do something like this:

Form buttons:

<div class="action">
  <%= submit_tag 'Save', :name => 'save_button' %>
  <%= submit_tag 'Continue', :name => 'submit' %>
</div>

Note the names of each submit button.

In the controller, you can reference each of the buttons individually, e.g.,

params[:save_button]
params[:submit]

You can then include specific logic in the create method for each button, perhaps in a if/elsif/else statement.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜