[英]Send form params to controller without a model in Rails
I have a form: 我有一个表格:
<%= form_for(:user_email, :url => {:action => 'send_email'}) do |f| %>
<%= f.text_field(:subject, class: 'form-control')%>
<%= f.button "Submit", type: 'submit'%>
<% end %>
And a controller: 和一个控制器:
class UserEmailController < ApplicationController
def send_email
p params[:subject]
end
end
This writes nil
in the console instead of the text I put in my textfield. 这将在控制台中写入
nil
而不是我在文本字段中输入的文本。
Question: How can I access the content of my fields in the controller? 问题:如何访问控制器中字段的内容?
I should mention that the action itself is triggered, but the parameters are nil. 我应该提到动作本身是触发的,但是参数为nil。
Use form_tag with your path. 在路径中使用form_tag 。 I have used
/user_email/send_email
. 我已经使用了
/user_email/send_email
。 change it according to the route your have setup in routes.rb
根据您在
routes.rb
设置的路由进行routes.rb
<%= form_tag('/user_email/send_email') do %>
<%= text_field_tag(:subject, class: 'form-control')%>
<%= submit_tag "Submit"%>
<% end %>
Try instead: 请尝试:
p params[:user_email][:subject]
And if that doesn't produce what you're looking for, try: 如果这不能满足您的需求,请尝试:
p params
... to inspect what's getting sent over the wire. ...检查通过电线发送的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.