Ask A Question

Notifications

You’re not receiving notifications from this thread.

Stuck on creating new record

Nelson asked in Rails

I'm not sure why I'm so lost on this one, I dont rally have a ton of experience with Rails but I did a blog with basic CRUD functionality and that was all good but this new project its just giving me a massive headache!

The app is very small, it uses Devise for auth so the idea is that a user creates an account and then from that account it can have a dashboard display information... that bit is working fine... the problem comes when I want the user to add new records to an 'Accounts' table...

Database schema is like this:

  • Users (devise)
  • Accounts
  • Usage

So every user can create (and have) many accounts and every account can have many (Usage[s])

My routes:

Rails.application.routes.draw do
  devise_for :users
  root 'home#index'

  resources :account do
    resources :usage
  end
end

The accounts_controller.rb

class AccountController < ApplicationController
  def new
    @account = Accounts.new
  end

  def create
    @account = Account.new(
      :user_id => current_user.id,
      :user_name => account_params[:user_name],
      :password => account_params[:password])
    if @account.save
      render root_path
    end
  end

  private
  def account_params
    params.require(:account).permit(:user_name, :password)
  end
end

The new.html.erb for Accounts

<h1>Add account</h1>

<%= form_for @account, account_path do |f| %>
  <%= f.text_field :user_name, placeholder: 'Username' %>
  <%= f.password_field :password, placeholder: 'Password' %>
  <br /><br />
  <%= f.submit 'Create', class: 'waves-effect waves-light btn' %>
<% end %>

The error I'm getting:

***ActionController::UrlGenerationError in Account#new
Showing /app/views/account/new.html.erb where line #7 raised:

No route matches {:action=>"show", :controller=>"account"}, missing required keys: [:id]*

I tried to nest my routes for Accounts inside the Users one and then I can create the account but the URL looks ugly...

I dont want /users/1/accounts/new its just not right because why would I want to display my current user ID... i want /accounts/new that one makes sense...

Any help please.

Reply

Hey Nelson,

You should be able to just remove account_path from form_for. Rails is smart enough to parse the correct paths from the object as long as you stick to the rails convention.

https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

Reply

Hi Jacob! Thanks for your reply... I just did and error mutated into:

**NoMethodError in Account#new
Showing /app/views/account/new.html.erb where line #7 raised:

undefined method 'accounts_path' for #<#< Class:0x00007fea9b19aa40 >:0x00007fea9944e478>
Did you mean? account_path**

(?)

Reply

Can you verify your new method? Above its Accounts.new (plural) but it should be Account.new (singular)

You also have class AccountController < ApplicationController which should use the plural form class AccountsController < ApplicationController

Check https://alexander-clark.com/blog/rails-conventions-singular-or-plural for an outline of the rails naming conventions.

Are you using the rails scaffold generator to generate your models, controllers, and views? You'll want to read up on them if you haven't, it will show you the "rails" way of doing things while working on your project.

Reply

Thanks Jacob... I havent been using the scaffold generator, but I will do in the future to avoid these confusions.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.