简体   繁体   中英

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:

In my rails app i have users which on the index site, called home, are displayed a couple of events.

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)

This all works fine, and this is how I have implemented the add functionality:

index.html.erb extract for home:

<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.

Now I would like to allow the user to add a comment along to the event to the 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.

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. So I created a new form... I can't get this form to pass the text_field information as parameters though:

new index.html.erb extract for home:

<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. Checking the post transmission in my browser it doesn't seem to transmit the :event_comment.

It can only access the :event_id and :user_id in the parameters.

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.

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]

Does anyone know how I can access the Form Data? Using params[:homepredictions] doesn't work in my event_items controller.

Found a solution now:

The :event_comment was being transmitted on submit. It is stored in Form Data as event_items[homeprediction] .

This is what I did in the event_items controller to access the data:

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. And in the new comment[] hash I was able to access the :event_comment passed from the form field_tag.

Don't know if this is the most elegant/right way of doing it. But it works for now :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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