Undefined Method 'Model_Name' for Project:Class

undefined method `model_name' for Project:Class

if Project is not an active record subclass, you need these and you can use form_for

class Project
extend ActiveModel::Naming
include ActiveModel::Conversion
def persisted?
false
end
...
end

view:

<%= form_for(@newproject)  do |f| %>
<%= f.label :name %>:
<%= f.text_field :description %><br />
<% end %>

undefined method `model_name' for { ... }

After a few days of playing around with the code, here's what worked.

form

<%= form_with(model: setting, scope: :settings, method: :post, multipart: true) do |f| %>

controller

def index
settings = {}

Setting.all.each do |setting|
settings[setting.name] = setting.value
end

@settings = OpenStruct.new(settings)
end

def update
@errors = []
@error_list = []

params[:settings].each do |name, value|
setting = Setting.find_by(name: name)
next unless setting.present?

setting.update(value: value)
end

redirect_to settings_path, notice: 'Settings have been updated'
end

Now to get the errors and other fields working.

Rails Undefined Method 'model_name'

Besides the correct route in your config/routes.rb, you will also need these two instructions on your model:

include ActiveModel::Conversion
extend ActiveModel::Naming

Take a look at this question: form_for without ActiveRecord, form action not updating.

For the route part of these answer, you could add this to your config/routes.rb:

resources :contacts, only: 'create'

This will generate de following route:

contacts POST /contacts(.:format)  contacts#create

Then you can use this action (contacts#create) to handle the form submission.

Undefined method 'model_name'

What path are you hitting when you are seeing this error?

If you are navigating to any path other than /subscribers/new then @subscriber will be nil and the form will throw the error that you are seeing. You are rendering a form via a partial in your view layout, that layout is rendered (presumably) throughout the app. Thus @subsriber won't always be set.

undefined method model_name

Try adding this to your presences_controller in the new or other relevant action that is rendering the form:

#presuming your model is called Presence
@presence = Presence.new

undefined method `model_name' for Address:Class

According to the Perpetuity README docs "you have to include ActiveModel::Model in your objects that you want to pass to various Rails methods (such as redirect_to, form_for and render)."

Try this and it should work as expected:

class Address
attr_accessor :city, :county, :line1, :line2, :postcode
include Perpetuity::RailsModel
end

Again from the README "This will let Rails know how to talk to your models in the way that Perpetuity handles them."

model_name is an ActiveModel method so without including this you will be unable to access these methods properly and trust me many rails gems use model_name to handle situations pertaining to routing.

Update

This is based on the question the comments about the errors method

I do not see an errors method anywhere in Perpetuiy. I think you would have to implement your own structure for this.

Also save will not return false ever it just updates the attributes without validation so you may have to rethink your design.

Actually looking at the change log validations were completely removed in 1.0.0.0beta

Like you said you included ActiveModel::Errors which you could possibly handle like this.

def errors
@errors ||= ActiveModel::Errors.new(self)
end

def save
validate!
errors.empty? ? super : false
end

def validate!
#place custom validations here e.g.
errors.add(:name, "cannot be blank.") if self.name = ""
errors.add(:number,"must be less than 7.") if self.number >= 7
end

Please note custom validations will not be standard Rails validations since these are not included either and if you keep moving down the inclusion path pretty soon you'll end up back at ActiveRecord

Also Note I do not and have not used Perpetuity so these are guidelines and I make no representation that they will work 100% correctly

Ruby on Rails: undefined method `model_name' for NilClass:Class

Your create action is rendering the new view again. However, @project_technol is not defined within the create action. The fields_for method calls model_name method on the argument passed in (@project_technol), but since @project_technol = nil, it's throwing that error. To fix this, within your create action, change

@project.projecttechnols.build(:technol_id => technol)

to

@project_technol = @project.projecttechnols.build(:technol_id => technol)

form_for undefined method `model_name' error

try this

# in your recipes_controller.rb
def new
@recipe = Recipe.new
end

and remove
match '/createrecipe', to: 'recipes#new'

#in routes.rb 
Baserecipe::Application.routes.draw do
resources :users
resources :recipes
....

see more info about route

your model file look like - recipe.rb

class Recipe < ActiveRecord::Base
...
...
end


Related Topics



Leave a reply



Submit