Oliver Oliver - 5 months ago 13
Ruby Question

Store data in join table the right way in Rails

I'm building an app in which a user can choose from a list of nutrients to add some of them in his personal food diary. Therefore I created a user_nutrients table that holds the user_id, nutrient_id and other information that the user has to type in.

Next to each nutrient item in the list I have a button ("Add this") that redirects the user to the new action of my

user_nutrients_controller.rb
. So far so good.

I researched now for a few days but did not find a suitable solution to my problem: what do I have to change in my code so that in the
user_nutrients/new.html.erb
the user does not have to type in the
nutrient_id
manually but that it automatically holds the id of the nutrient the user selected before in the
nutrients/index.html.erb
list (through the "Add this" button)?

Every user
has_many :user_nutrients
and
has_many :nutrients, through: :user_nutrients
. Every nutrient
has_many :user_nutrients
and
has_many :users, through: :user_nutrients
.

Thanks a lot.

Sorry if this question is too simple or not fully understandable. Just started coding few weeks ago.

nutrients/index.html.erb

<% @nutrients.each do |nutrient| %>
<tr>
<td><%= nutrient.id %></td>
<td><%= link_to nutrient.name, nutrient, class: "link" %></td>
<td class="hidden-xs"><%= nutrient.kcal %></td>
<td class="hidden-xs"><%= nutrient.fat %></td>
<td class="hidden-xs"><%= nutrient.carbs %></td>
<td class="hidden-xs"><%= nutrient.sugar %></td>
<td class="hidden-xs"><%= nutrient.protein %></td>
<td class="hidden-xs"><%= nutrient.gram %></td>
<td class="hidden-xs"><%= nutrient.points %></td>

<td>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle btn-xs" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Actions
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenu1">
<li><%= link_to 'Edit', edit_nutrient_path(nutrient) %></li>
<li role="separator" class="divider"></li>
<li><%= link_to 'Add this', new_user_nutrient_path %></li>
<li role="separator" class="divider"></li>
<li><%= link_to 'Delete', nutrient_path(nutrient), method: :delete, data: {confirm: "Are you sure?"} %></li>


user_nutrients_controller.rb

class UserNutrientsController < ApplicationController
before_action :set_user_nutrient, only: [:show, :edit, :update, :destroy]

# GET /user_nutrients
# GET /user_nutrients.json
def index
@user_nutrients = UserNutrient.where(:user_id => current_user.id).order(day: :desc)
end

# GET /user_nutrients/1
# GET /user_nutrients/1.json
def show
end

# GET /user_nutrients/new
def new
@user_nutrient = UserNutrient.new
end

# GET /user_nutrients/1/edit
def edit
end

# POST /user_nutrients
# POST /user_nutrients.json
def create
@user_nutrient = UserNutrient.new(user_nutrient_params)
@user_nutrient.user = current_user
respond_to do |format|
if @user_nutrient.save
format.html { redirect_to user_nutrients_path, notice: 'User nutrient was successfully created.' }
format.json { render :show, status: :created, location: @user_nutrient }
else
format.html { render :new }
format.json { render json: @user_nutrient.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /user_nutrients/1
# PATCH/PUT /user_nutrients/1.json
def update
respond_to do |format|
if @user_nutrient.update(user_nutrient_params)
format.html { redirect_to @user_nutrient, notice: 'User nutrient was successfully updated.' }
format.json { render :show, status: :ok, location: @user_nutrient }
else
format.html { render :edit }
format.json { render json: @user_nutrient.errors, status: :unprocessable_entity }
end
end
end

# DELETE /user_nutrients/1
# DELETE /user_nutrients/1.json
def destroy
@user_nutrient.destroy
respond_to do |format|
format.html { redirect_to user_nutrients_url, notice: 'User nutrient was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user_nutrient
@user_nutrient = UserNutrient.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def user_nutrient_params
params.require(:user_nutrient).permit(:user_id, :nutrient_id, :amount, :day)
end
end


_form.html.erb (for user nutrients)

<%= form_for(@user_nutrient) do |f| %>
<% if @user_nutrient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user_nutrient.errors.count, "error") %> prohibited this user_nutrient from being saved:</h2>

<ul>
<% @user_nutrient.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :nutrient_id %><br>
<%= f.number_field :nutrient_id %>
</div>
<div class="field">
<%= f.label :amount %><br>
<%= f.number_field :amount %>
</div>
<div class="field">
<%= f.label :day %><br>
<%= f.date_select :day %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Answer

You want to pass the nutrient_id to the new action in the UserNutrientsController. You could pass it via querystring like this in nutrients#index:

link_to 'Add this', new_user_nutrient_path(nutrient_id: nutrient.id)

The new action would look like this:

def new
  @nutrient = Nutrient.find(params[:nutrient_id])
  @user_nutrient = UserNutrient.new(nutrient_id: @nutrient.id)
end

The form would looksomething like this:

<h2>Add Nutrient <%= @nutrient.name %></h2>

<%= form_for(@user_nutrient) do |f| %>

  # get rid of number_field for nutrient_id and use a hidden field
  <%= f.hidden_field :nutrient_id %>
Comments