我对铁轨上的红宝石很新!我基本上试图创建一个允许组织创建事件的应用程序!到目前为止,用户能够创建一个组织但是当他们尝试创建一个事件时,我得到了这个错误:路由错误未定义的局部变量或方法`params' for EventsController:Class
如果您有任何建议,我会查看所有内容并试图找到类似于堆栈溢出的类似内容,请告诉我们!
事件控制器: before_action @organization = Organization.find(params [:organization_id])
def index
@events = @organization.events
end
def show
@event = Event.find(params[:id])
end
def new
@event = Event.new
end
def create
@event = Event.new(event_params)
@event.organization = @organization
if @event.save
redirect_to organizations_path
else
render 'new'
flash[:notice] = "Event not saved"
end
end
def edit
@event = Event.find(params[:id])
end
def destroy
@event = Event.find(params[:id])
@event.destroy
redirect_to organizations_url
end
private
def event_params
params.require(:event).permit(:user_id, :organization_id, :event_size, :date, :name, :description, :location)
end
end
组织控制器
def index
@organizations = Organization.all
# @organization = Organization.find(params[:id])
end
def show
@organization = Organization.find(params[:id])
@events = @organization.events
end
def new
@organization = Organization.new
end
def create
@organization = Organization.new(organization_params)
if @organization.save
redirect_to organizations_path
else
render "new"
end
end
private
def organization_params
params.require(:organization).permit(:organization_id, :name, :description, :location, :volunteer_position)
end
end
事件index.html.erb
<h1>Events index</h1>
<% @events.each do |event| %>
<div class="events_name">
<p><%= event.name %></p>
<p><%= event.description %></p>
<p><%= event.location %></p>
<br/>
<% end %>
</div>
路由
Rails.application.routes.draw do
root 'home#index'
resources :timeslots
resources :sessions, only: [:new, :create, :destroy]
resources :users
resources :home
resources :home, only: [:index]
resources :organizations do
resources :events
end
活动表格
<%= form_for @event do |f| %>
<div>
<%= f.label :name %>:
<%= f.text_field :name %><br />
</div>
<div>
<%= f.label :description %>:
<%= f.text_field :description %><br />
</div>
<div>
<%= f.label :location %>:
<%= f.text_field :location %><br />
</div>
<div>
<div class="field">
<%= f.label :date%>
<%= f.date_select :date,
{ order: [:month, :day, :year],
prompt: [ day: 'Select day', month: 'Select month', year: 'Select year' ]} %>
</div>
<br>
<%= f.submit %>
<% end %>
答案 0 :(得分:0)
根据您对第一条评论的回复,您可能需要继承ApplicationController
,这是params
定义的地方。
在构建应用程序时,所有控制器都需要以某种方式从ApplicationController
继承。有时,这意味着嵌套控制器将继承自继承自ApplicationController
的父控制器。
如,
class ParentController < ApplicationController
end
class Parent::ChildController < ParentController
end