[英]Rails link_to controller action
I am creating a rails CRUD application for practice. 我正在创建一个Rails CRUD应用程序进行练习。 Here is the contents of my routes.rb:
这是我的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’
My new event page currently employs a Create button, see code for new.html.erb below: 我的新活动页面当前使用“创建”按钮,请参见下面的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>
I want to replace the Create button with a Create link while still maintaining the rest of the code in new.html.erb 我想用“创建”链接替换“创建”按钮,同时仍保留new.html.erb中的其余代码
I can't seem to figure out how to create a link_to that submits the data collected from the text boxes on the page. 我似乎无法弄清楚如何创建link_to来提交从页面上的文本框中收集的数据。 I am able to statically submit information using the below code:
我可以使用以下代码静态提交信息:
<%= link_to 'Create', events_path(:event => "Genesis", :venue => "Blossom", :start_date => "2017-01-01", :end_date => "2017-01-02", :description => "2017 Tour") :method => :post %>
You can do that with Jquery : 您可以使用Jquery做到这一点:
$(document).on("click", "#your_link", function(){
$("#form").submit();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.