Я сделал сайт по продаже кресел, в данный момент я хочу редактировать пост председателя, независимо от того, кто его разместил.однако, это приводит к ошибке 'неопределенный метод `имя_модели' для nil: NilClass '.Не уверен почему.Я пытался только с @chair в форме редактирования.Я добавил еще немного кода из моделей и терминала ниже.
Started GET "/chairs/1/edit" for 127.0.0.1 at 2018-11-27 20:30:53 +0000
Processing by ChairsController#edit as HTML
Parameters: {"id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 3], ["LIMIT", 1]]
↳ /Users/benherring/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Rendering chairs/edit.html.erb within layouts/application
Rendered chairs/edit.html.erb within layouts/application (3.4ms)
Completed 500 Internal Server Error in 22ms (ActiveRecord: 0.4ms)
ActionView::Template::Error (undefined method `model_name' for nil:NilClass):
1: <%= simple_form_for @chair do |f| %>
2: <%= f.input :name %>
3: <%= f.input :description %>
4: <%= f.submit :submit %>
app/views/chairs/edit.html.erb:1:in `_app_views_chairs_edit_html_erb___2826282127562707280_70360397910180' .
class Chair < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :chairs
end
class ChairsController < ApplicationController
def index
@chairs = Chair.all
end
def show
@chair = Chair.find(params[:id])
@user = @chair.user
end
def new
@chair = Chair.new
@user = current_user
end
def create
@user = current_user
@chair = Chair.new(chair_params)
@chair.user = @user
if @chair.save
redirect_to chairs_path
end
def edit
@chair = Chair.find(params[:id])
@user = current_user
end
def update
@chair = Chair.find(params[:id])
@chair.update(chair_params)
redirect_to chairs_path
end
def delete
end
end
private
def chair_params
params.require(:chair).permit(:name, :description)
end
end
<%= simple_form_for [@user, @chair] do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit :submit %>
<% end %>