1

I'm struggling out with the Rails button_to.

Essentially, I'm implementing something like a food diary, to keep track of the calories, proteins, etc of the food a user takes in a day. I have my nutrition page in which the user clicks on add item. This opens up a bootstrap modal in which the user types on a search bar, and hits on search. This takes them to a results page that shows up to 20 different products in which the users can click on each one and a modal with details of the clicked product will appear. On this modal, I also include a button to add that item to their diary,and this button should call a controller method called createmeal

def createmeal(product, quantity)
        @user = current_user
        @user.journal_date = Time.now
        food = FoodItem.new(:brand => product.brand,
                            :item =>  product.item,
                            :qty => product.qty,
                            :unit => product.unit,
                            :cal => product.cal,
                            :carb => product.carb,
                            :sod => product.sod,
                            :dfib => product.dfib,
                            :prot => product.prot,
                            :totalfats => product.totalfats,
                            :satfats => product.satfats,
                            :sugars => product.sugars,
                            :chol => product.chol,
                            :iron => product.iron,
                            :calcium => product.calcium)
        food.save
        meal = Meal.new(:user_id => @user.id, :food_item_id => food.id, :date => @user.journal_date, :meal_type => Rails.cache.read('meal-type'), :qty => quantity)
        meal.save

    end

The button add, should also return the user to the nutrition page, so I tried something like this:

<div class="header_texture_tracking"></div>
<div class="landing_main_quote_tracking">TRACK</div>
<div class="header_subtitle_tracking">nutrition, fitness and progress</div>

<%= form_tag searchresults_path, :method => 'get' do %>
  <p style=" position: absolute;
          top: 300px;
          left: 500px;"> 
    <%= text_field_tag :search, params[:search], inputhtml: {:class => 'searchtf'}%>
    <%= submit_tag "Search", :name => nil, inputhtml: {:class => 'btn btn-primary'} %>
 </p>
<% end %>   


<div class="resultsdiv">
  <% if @prods %>
      <p style="font-family: 'Lane - Narrow';
            color: rgb( 78, 83, 82 ); 
            font-size: 20px;
            position: absolute;
            left: -180px;">
            Showing  search results for <%= @search %>: 
      </p>
      <%  @prods.each do |f| %>
        <a  data-toggle="modal" href=<%="#"+"#{f.id}"+"item-details"%>><div class="results_box">
        <p class="itemresult"><%= f.item %></p><br/><br/>
        <p class="itemdetails"><%= f.brand %>, <%= f.qty %><%= f.unit  %>, <%= f.cal %> calories</p>
    </div></a>
    <% end %>
    <%= @search = '' %>
 <% end %>

<% if @prods %>
    <%  @prods.each do |f| %>
        <div class="modal fade" id=<%="#{f.id}"+"item-details" %> >

            <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id = 'resultsDetailsHeaderBox'><p class="resultsDetailsName"><%= f.item %></p></h4>
                </div>
                    <div class="modal-body">
                        Brand: <%= f.brand %> 
                        Serving size: <%= f.qty %><%= f.unit  %>
                        Calories: <%= f.cal %> 
                   </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

                    <%= button_to 'ADD', nutrition_path, {action: "createmeal(f,1)"}%>

                </div>
            </div>
        </div>
    </div>
 <% end %>

In the end I've tried different combinations, including the use of helper methods, however as it is right now, it just doesn't call the method createmeal at all. If I try it like this:

<%= button_to 'ADD', nutrition_path, {action: createmeal(f,1)}%>

it calls the method for all the products in the results page, and not the one that is selected in the modal.

Do you have any suggestions on how to make it so that the method is called only for the selected product and redirect afterwards to the nutition page?

RodMal
  • 11
  • 4
  • 1
    Parameters are passed differently to controllers, via the `params` hash. Function arguments in controller actions are mostly useless. Arguments to an action should be listed in hash instead of `action`. `action`, in turn, should be derermined by routes and HTTP verb used (`button_to` uses POST by default). This is more of guidance than an actual answer, there's really a lot to fix. – D-side Sep 15 '14 at 18:57
  • I would love to help you but like @D-side said "there's a lot to fix". I would read up on Rails, ORM and OOP for start as I am not sure based on this post that you are truly grasping how it works or how to harness it appropriately. I started to write an answer and every time I finished one part I realized there was something else that should be fixed as well so until you grasp the basics unfortunately it will be difficult to help you move forward. – engineersmnky Sep 15 '14 at 20:37

0 Answers0