开发者

Rails route for button

开发者 https://www.devze.com 2023-04-10 02:56 出处:网络
I\'m a rails newbie and a question on routes is confusing me. On one of my pages, I have a form.In that form I allow the user to fill in some needed information and press a \"submit\" button.

I'm a rails newbie and a question on routes is confusing me.

On one of my pages, I have a form. In that form I allow the user to fill in some needed information and press a "submit" button.

I get:

No route matches {:action=>"inventory_test", :controller=>"test_types"}

I do have an action in the test_type controller for "inventory_test".

My confusion is that routes seem to be defined according to the REST model, such as /Users/edit/1. That's fine, but how does one create routes for things like buttons?

I may be naive, but it seems like if I tried to setup a route in the form:

match 'some/url' => 'controller#action'

then I'm essentially defining the action for the entire page. How do I define actions for elements on the page?

When this button is clicked, I want the action in the controller called. I'm looking for:

match "submit_button" => 'test_types#inventory_test'

I realize I'm likely misunderstanding the paradigm, so any education is greatly appreciated.


Code: (Note that I haven't tested the form code yet, but hopefully you get the idea)

index.html.haml

%div

  %table
    %caption 
      Inventory Tests
      %form
        Inven开发者_StackOverflow社区tory Run: %input {:type => 'text', :name=>'inventory_run'}
        Inventory Class: %input {:type => 'text', :name=>'inventory_class'}
        =button_to "Run Inventory Test", :action => 'inventory_test';


If you are submitting a normal form then this should help

The route match '/url' => "test_types#inventory_test" should work fine.

<%= form_for(@user, :url => "/url") do |f| %>
  "Put your form code here"
  <%= f.submit "Submit" %>
<% end %>

Revert back if any queries .

Edited as per code posted

%form{ :action => "inventory_test", :method => "post"}

    %label{:for => "inventory_run"} Inventory Run:
    %input{:type => "text", :name => "inventory_run"}

    %label{:for => "inventory_class"} Inventory Class:
    %input{:type => "text", :name => "inventory_class"}

%input{:type => "submit", :value => "Submit"}

Just check, it should work for you but i have not tried with it

0

精彩评论

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