1

I'm getting a variety of errors when trying to create a new object, 'Investor' using form_for.

I set it up as simple as possible with only one attribute (:name) but I do not think I'm passing the arguments correctly... or maybe a template needs to be defined in a create.html.rb file? ...or something should be added to config/routes for defining what it means to create, but I'm not really sure what's the solution or what I'm doing incorrectly, although it could be something basic... but I can't seem to solve it or find the proper way to use the form_for to create new objects with multiple params/ args. I was getting the error - Investor/create "template is missing" so I wonder if I need to create a template or not... ?

At the moment I'm having an error in def new @investor = Investor.new, which shouldn't even be used because I'm trying to create an object with arguments rather than one without any, but when I take out the def new from the controller, I get a totally different error saying that there's an argumentError (rb:10)in the form_for line "First argument in form cannot be nil or empty" This is one of the error I had before so I'm starting to feel like I'm spinning my wheels on this, but when I follow the examples I'm getting errors, so maybe someone with more experience could point out my mistakes.

Otherwise, when I send it like Investor.new(:name) or "name" I get a missing stringify_method error, so maybe the problem I'm having is that I don't know the correct way to pass multiple arguments in the controller, but even when I try to just do it with only one there still seems to be a problem creating a new Investor object the way I have it set. Any ideas?

Which is more correct / what's the difference between?: def create Investor.create(params[]) end def create Investor.new(params[]) end

I couldn't find any simple examples of how to use the controllers and the model to create the objects using form_for. The tutorial shows simple examples of how to create user objects, but I think the process and the syntax is a bit different when you generate the user model first to create the migrations and then use form_for to create objects of new Users, Investors, or any other entities.

Many details seem to be left out in the online examples and since there are many different ways to do this apparently (tutorial uses (name, options {}) as one example and other examples use args*), well, I couldn't get it to work just right using this scaled back simple example I created so I thought I'd post it up here for some help.

Thanks! Kameron

Controller:

class InvestorsController < ApplicationController
respond_to :html

def new
    @investor = Investor.new
end

def create
    @investor = Investor.create(investor_params)
end

def investor_params
    allow = [:name]
    params.require(investor).permit(allow)
end
end

Model:

class Investor < ActiveRecord::Base
    include ActiveModel::Validations
       attr_accessor :name
         validates :name, presence: true    
    end
end

<%= form_for @investor do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.submit "Create my account" %>
<% end %>

Routes:

Tycoon::Application.routes.draw do
resources :investors, only: [:new,:create]
root 'static_pages#home'
match '/help',    to: 'static_pages#help',    via: 'get'
match '/investors/new', to: 'investors#new',    via: 'get'
match '/about',   to: 'static_pages#about',   via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
get "static_pages/home"

1 Answer 1

1

Try this. Controller:

class InvestorsController < ApplicationController
  respond_to :html

  def new
    @investor = Investor.new
  end

  def create
    @investor = Investor.create(investor_params)
  end

  private

  def investor_params
    params.require(:investor).permit(:name)
  end
end

Model:

class Investor < ActiveRecord::Base
  validates :name, presence: true    
end

View:

<%= form_for @investor do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.submit "Create my account" %>
<% end %>

Routes:

Tycoon::Application.routes.draw do
  resources :investors, only: [:new,:create]
  root 'static_pages#home'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'
  get "static_pages/home"
end
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Anton, I edited it down like you suggested and the form page still displays, but when I submit a name I get an error saying Missing template investor/create, application/create with {:locale=[en], :formats......]} Searched in:* "/home/...app/views"
Does it require a def initialize function? When I add that I g a wrong argument error, not sure which direction to go with this now? Thanks for the help
It just says that there is no view for your InvestorsController#create action. To fix this you should create app/views/investors/create.html.erb file. Or add to the end of your #create action something like redirect_to root_path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.