简体   繁体   English

将text_field输入值传递给某个控制器

[英]Passing text_field input value to a certain controller

I am very new to Ruby and Rails, and I have come across a problem I can't seem to be able to solve: 我是Ruby和Rails的新手,遇到了一个我似乎无法解决的问题:

In my rails app i have users which on the index site, called home, are displayed a couple of events. 在我的rails应用程序中,我有一些用户在索引站点(称为home)上显示了几个事件。

If the user wants to "bookmark" these events he can click on a button next to the event an add it to his event_items list, which is just an "association"-controller I implented for the association between users and events (eg users has_many events and vice versa) 如果用户想“标记”这些事件,则可以单击事件旁边的按钮,将其添加到他的event_items列表中,这只是我为用户和事件之间的关联而设置的“关联”控制器(例如,用户has_many事件,反之亦然)

This all works fine, and this is how I have implemented the add functionality: 一切正常,这就是我实现添加功能的方式:

index.html.erb extract for home: index.html.erb提取主页:

<h2>Events</h2>
<% @events.each do |event| %>​
​  <div>​<%= event.eventtitle %></div>
  <%= button_to 'Add Event', event_items_path(event_id: event, user_id: session[:user_id]) %>
<% end %>

The button_to method works fine to add an event to the event_item list which just records the user_id and the event_id for an association in an even_items db-table. button_to方法可以很好地将事件添加到event_item列表,该列表仅将even_items db表中的关联记录为user_id和event_id。

Now I would like to allow the user to add a comment along to the event to the event_items db-table. 现在,我想允许用户向事件添加一个注释到event_items db-table。

I can't seem to find a way of passing a text_field value to my event_items_path to add the comment to the event_items table in the db. 我似乎找不到一种将text_field值传递到event_items_path,以将注释添加到数据库中的event_items表的方法。

I guess I can't use the "button_to" method anymore because it is resolved in it's own form and thus I can't transmit any text_field information. 我猜我不能再使用“ button_to”方法,因为它以其自身的形式解析,因此我无法传输任何text_field信息。 So I created a new form... I can't get this form to pass the text_field information as parameters though: 因此,我创建了一个新表单...尽管无法通过此表单将text_field信息作为参数传递:

new index.html.erb extract for home: 用于首页的新index.html.erb提取:

<h2>Events</h2>
<% @events.each do |event| %>​
  <%= form_for :event_items, url: event_items_path(event_id: event, user_id: session[:user_id]) do |f| %>
    <div>​<%= event.eventtitle %></div>
    <%= f.label :event_comment %><br />        
    <%= f.text_field :event_comment %>
    <%= f.submit %>
  <% end %>
<% end %>

When I press the submit button, it still creates the event_item in my db-table, but withouth the event_comment. 当我按下提交按钮时,它仍然在我的数据库表中创建event_item,但是没有event_comment。 Checking the post transmission in my browser it doesn't seem to transmit the :event_comment. 在我的浏览器中检查帖子的发送似乎没有发送:event_comment。

It can only access the :event_id and :user_id in the parameters. 它只能访问参数中的:event_id和:user_id。

I would appreciate any help! 我将不胜感激任何帮助!

Update 更新资料

I have just now realised, that when pressing the submit button from my new form above the :event_comment does get transmitted with the form. 我刚刚意识到,当从我的新表单上方按提交按钮时,:event_comment确实与表单一起发送。

Checking in Chromes DevTools, in Network>>Headers there is a post request which has event_id and user_id listed under Query String Parameters and the event_comment listed under Form Data as event_items[homeprediction] 在Chromes DevTools中,在Network >> Headers中检查一个发布请求,该请求的event_iduser_id在“查询字符串参数”下列出,而event_comment在“表单数据”下作为event_items[homeprediction]

Does anyone know how I can access the Form Data? 有谁知道我如何访问表单数据? Using params[:homepredictions] doesn't work in my event_items controller. 在我的event_items控制器中,无法使用params [:homepredictions]。

Found a solution now: 现在找到一个解决方案:

The :event_comment was being transmitted on submit. :event_comment在提交时被传输。 It is stored in Form Data as event_items[homeprediction] . 它作为event_items[homeprediction]存储在表单数据中。

This is what I did in the event_items controller to access the data: 这是我在event_items控制器中所做的,用于访问数据:

def create
  @user = User.find(params[:user_id])
  event = Event.find(params[:match_id])
  comment = params[:event_items]
  @event_item = @user.event_items.build(event: event, event_comment: comment[:event_comment])
  ...
end

I saved the params[:event_items] hash to a new comment[] hash. 我将params[:event_items]哈希保存到新的comment[]哈希中。 And in the new comment[] hash I was able to access the :event_comment passed from the form field_tag. 在新的comment[]哈希中,我可以访问从field_tag形式传递的:event_comment

Don't know if this is the most elegant/right way of doing it. 不知道这是否是最优雅/最正确的方法。 But it works for now :-) 但这暂时有效:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM