PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] how to properly create and update posts with active record association?

 October 21, 2022     has-many, rails-activerecord, ruby-on-rails     No comments   

Issue

So I have this challenge that I'm having trouble figuring out how to declare the controllers so it works properly, I've been having a diverse kind of errors but my major issue is that, the way it is now I can't save a new recipe or update the 'Tipo de Receita' which is associated with the recipe_type model used on a drop down, bear in mind that recipe_type.name will be populated beforehand

that's the edit.hmtl.erb that do the editing

<%= form_for @recipe do |f| %>
  <%= f.label :title, 'Título' %>
  <%= f.text_field :title %>
  <%= f.label :recipe_type, 'Tipo da Receita' %>
  <%= collection_select(:recipe, :recipe_type_id, RecipeType.all, :id, :name) %>
  <%= f.label :cuisine, 'Cozinha' %>
  <%= f.text_field :cuisine %>
  <%= f.label :difficulty, 'Dificuldade' %>
  <%= f.text_field :difficulty %>
  <%= f.label :cook_time, 'Tempo de Preparo' %>
  <%= f.number_field :cook_time %>
  <%= f.label :ingredients, 'Ingredientes' %>
  <%= f.text_area :ingredients %>
  <%= f.label :cook_method, 'Como Preparar' %>
  <%= f.text_area :cook_method %>
  <%= f.submit 'Enviar' %>
<% end %>

similarly that's the new.hmtl.erb that registers the new recipes

<%= form_for @recipe do |f| %>
  <%= f.label :title, 'Título' %>
  <%= f.text_field :title %>
  <%= f.label :recipe_type, 'Tipo da Receita' %>
  <%= collection_select(:recipe, :recipe_type_id, RecipeType.all, :id, :name) %>
  <%= f.label :cuisine, 'Cozinha' %>
  <%= f.text_field :cuisine %>
  <%= f.label :difficulty, 'Dificuldade' %>
  <%= f.text_field :difficulty %>
  <%= f.label :cook_time, 'Tempo de Preparo' %>
  <%= f.number_field :cook_time %>
  <%= f.label :ingredients, 'Ingredientes' %>
  <%= f.text_area :ingredients %>
  <%= f.label :cook_method, 'Como Preparar' %>
  <%= f.text_area :cook_method %>
  <%= f.submit 'Enviar' %>
<% end %>

models/recipe_type which is the one used in the drop down

class RecipeType < ApplicationRecord
    has_many :recipes
    validates :name, presence: true
end

models/recipe which receives the recipe_type model

class Recipe < ApplicationRecord
  belongs_to :recipe_type
  validates :title, :cuisine, :difficulty, :cook_time,
            :ingredients, :cook_method, presence: true

  def cook_time_min
    "#{cook_time} minutos"
  end
end

recipes.controller.rb

class RecipesController < ApplicationController
  def index
    @recipes = Recipe.all
  end

  def show
    @recipe = Recipe.find(params[:id])
  end

  def new
    @recipe = Recipe.new
    @recipe_type = RecipeType.all
  end

  def create
    @recipe_type = RecipeType.all
    @recipe = Recipe.new(recipe_params)
    if @recipe.save
      redirect_to @recipe
    else
      flash[:alert] = 'Você deve informar todos os dados da receita'
      render :new
    end
  end

  def edit
    @recipe_type = RecipeType.all
    @recipe = Recipe.find(params[:id])
    @recipe_type = RecipeType.all
  end

  def update
    @recipe = Recipe.find(params[:id])
    if @recipe.update(recipe_params)
      redirect_to @recipe
    else
      flash[:alert] = 'Você deve informar todos os dados da receita'
      render :edit
    end
  end

  private

  def recipe_params
    params.require(:recipe).permit(:title, :cuisine, :difficulty,
                                   :cook_time, :ingredients, :cook_method, :name)
  end

end

and that's the schema

ActiveRecord::Schema.define(version: 2020_03_26_013134) do

  create_table "recipe_types", force: :cascade do |t|
    t.string "name"
    t.integer "recipe_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["recipe_id"], name: "index_recipe_types_on_recipe_id"
  end

  create_table "recipes", force: :cascade do |t|
    t.string "title"
    t.string "cuisine"
    t.string "difficulty"
    t.integer "cook_time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "ingredients"
    t.text "cook_method"
    t.integer "recipe_type_id"
    t.index ["recipe_type_id"], name: "index_recipes_on_recipe_type_id"
  end
end

Solution

One problem is that your not building the input from the form builder. You want to use:

<%= f.label :recipe_type_id, 'Tipo da Receita' # key has to match input for accessibility %>
<%= f.collection_select(:recipe_type_id, RecipeType.all, :id, :name) %>

collection_select is a bare bones input helper that will just create a select tag that's not bound to a form builder and thus not to your model instance. So if the record is invalid or you are updating an existing record it won't have anything selected.

f.collection_select is a method on the form builder and will properly set the recipe_type_id value from the model instance wrapped by the form.

You also need to permit the param in your whitelist:

def recipe_params
  params.require(:recipe)
        .permit(
          :title, :cuisine, :difficulty,
          :cook_time, :ingredients, :cook_method, 
          :name, :recipe_type_id
        )
end


Answered By - max
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing