简体   繁体   中英

Rails - Passing data from views to another controller

I'm fairly new to rails so any help would be much appreciated.

I have a page where it shows a list of food (each food has its own id). The list of food shown will differ depending on the restaurant selected (queried from db so its dynamic). In this page, users can input the quantity amount for each food that they want.

The following is the controllers and views that i have setup:

  • Controllers: Menu and Order Summary
  • Views: menu/show.html.erb and order_summary/index.html.erb

I want to display the quantity for each food that the user have entered in the order_summary/index.html.erb.

My question is after the user have entered the quantity inputs, how do i pass the input values together with the food id into the Order Summary controller?

Below is the snippet of the code for the view menu/show.html.erb

 <% @menu_appetizer.each do |appetizer| %> <div class="food-info"> <div class="food-title"><%= appetizer.name %></div> <p class="food-description"><%= appetizer.description %></p> </div> <div class="food-price"> <span class="price">RM<%= appetizer.price %></span> </div> <input type="number" min="0" class="quantity-input" data-price="<%=appetizer.price%>" data-food-id="<%=appetizer.id%>"> <% end %> <%= link_to 'CONTINUE', {:controller => "order_summary", :action => "index"} %> 

you have to make the forms(form_for) with food title and quantity as input tag and submit it to order controller.

OR

you can use AJAX on button click and pass data to order controller.

The index page is generally used for displaying every item in a given table (or at least a selection). It sounds like you're looking to display a specific order summary, so it would probably be better off going into order_summary/show.html.

This would mean a separate action in your order_summary controller where you get the order summary and give it to the view. If you have your routes set up in the standard way, you get the order summary from either params[:id] or params[:order_summary_id]:

def show
  @order_summary = OrderSummary.find(params[:id])
end

This allows you to access the @order_summary from your view. Depending on how you've set up your model, that means you could do say:

<% @order_summary.items.each do |item| %>
  <div class="food-info">
    <div class="food-title"><%= item.name %></div>
    <p class="food-description"><%= item.description %></p>
  </div>
  <div class="food-price">
    <span class="price">RM<%= item.price %></span>
  </div>
  <input type="number" min="0" class="quantity-input" data-price="<%=appetizer.price%>" data-food-id="<%=appetizer.id%>">
<% end %>

Which, assuming you have setup your order summary to have many items, will display the above info.

您应该使用AJAX传递对您的页面也有用的数据,因为使用AJAX可以获取数据而无需加载。

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