Home > Mobile >  Ruby on Rails failed to add a new data
Ruby on Rails failed to add a new data

Time:04-18

I'm making simple CRUD and the current goal is to add data. However, I found that I can't add any data, and the terminal log also shows "[Webpacker] Everything's up-to-date. Nothing to do", which means there is no error message.

According to my design in the controller, the new data must have failed, so I stopped at new.html.erb. I'm guessing it has something to do with the model's relationship.

This is model User

class User < ApplicationRecord
  has_many :reviews
  has_many :recipes, through: :reviews
end

This is the model Recipe

class Recipe < ApplicationRecord
  has_many :reviews
  belongs_to :user
end

This is model Review

class Review < ApplicationRecord
  belongs_to :user
  belongs_to :recipe
end

This is the RecipeController

class RecipesController < ApplicationController

  def index
    @recipes = Recipe.all  
  end

  def new
    @recipe = Recipe.new  
  end

  def create
    @recipe = Recipe.new(recipe_params)
    
    if @recipe.save
      redirect_to recipes_path, notice: "Successful!"
    else
      render :new
    end
  end

private

  def recipe_params
    params.require(:recipe).permit(:title, :money)
  end
end

this is the web page

<h1>Add New One</h1>

<%= form_for(@recipe) do |r| %>
  <%= r.label :title, "Title" %>
  <%= r.text_field :title%>

  <%= r.label :money, "Budget" %>
  <%= r.text_field :money %>

  <%= r.submit %>
<% end %>

<%= link_to "Back to list", recipes_path %>

CodePudding user response:

You should first add a callback to ensure that only signed in users can create recipes (unless you actually want to let anomynous users create/update/delete recipies).

For example with Devise you would use its authenticate_user helper which will bail and redirect to the sign in path if the user is not authenticated:

class RecipesController < ApplicationController
  before_action :authenticate_user!, only: [:new, :create]

  # ...
end

If you're reinventing the authentication wheel you should create a similiar method which is used to prevent access.

You would then initialize the resource off the current user:

class RecipesController < ApplicationController
  before_action :authenticate_user!, except: [:show, :index]

  def create
    @recipe = current_user.recipes.new(recipe_params)
    if @recipe.save
      redirect_to recipes_path, notice: "Successful!"
    else
      render :new
    end
  end
end

Here I am assuming that you have a current_user method which will retrieve the user based on an id stored the session.

Since you have an indirect assocation this will create a row in the reviews table with the users id and the recipe id as the record in the recipies table.

You also want to display the validation errors in the form so that the user gets feedback.

CodePudding user response:

You are probably right that it is a validation error related to the belongs_to relationship. You should display validation errors for your form as described here https://guides.rubyonrails.org/getting_started.html#validations-and-displaying-error-messages

  • Related