Rails link_to控制器动作

时间:2017-01-05 03:13:17

标签: ruby-on-rails ruby-on-rails-4

我正在为练习创建rails CRUD应用程序。这是我的routes.rb的内容:

get ‘/events’ => ‘events#index’
post ‘/events’ => ‘events#create’
get ‘/events/new’ => ‘events#new’, as: :’new_event’
get ‘/events/:id/edit’ => ‘events#edit’, as: :’edit_event’
get ‘/events/:id’ => ‘events#show’, as: :’event’
patch ‘/events/:id’ => ‘events#update’
put ‘/events/:id’ => ‘events#update’  
delete ‘/events/:id’ => ‘events#destroy’

我的新活动页面目前使用了创建按钮,请参阅下面的new.html.erb代码:

  <h1>New Event</h1>
  <%= link_to “Back”,”/events” %><br><br>
   <table border=”1”>
    <tr>
     <th>Event</th>
     <th>Venue</th>
     <th>Start Time</th>
     <th>End Time</th>
     <th>Description</th>
   </tr>
<%= form_for(@event) do |t| %> 
  <tr>
    <td><%= t.text_field :event %></td>
    <td><%= t.text_field :venue%></td>
    <td><%= t.date_field :start_date%></td>
    <td><%= t.date_field :end_date%></td>
    <td><%= t.text_area :description%></td>
     <td><%= f.submit “Create” %><br><br></td>
 </tr>
</table>

我想用Create链接替换Create按钮,同时仍然保留new.html.erb中的其余代码

我似乎无法弄清楚如何创建一个link_to来提交从页面上的文本框收集的数据。我可以使用以下代码静态提交信息:

<%= link_to 'Create', events_path(:event => "Genesis", :venue => "Blossom", :start_date => "2017-01-01", :end_date => "2017-01-02", :description => "2017 Tour") :method => :post %>

1 个答案:

答案 0 :(得分:0)

你可以用Jquery做到这一点:

$(document).on("click", "#your_link", function(){
   $("#form").submit();
});