我与地点和事件有关系has_and_belongs_to_many。当我创建一个事件时,不要保存places参数,而是保存其他参数。但是,当我想编辑一个事件时,这不会保留参数。 (图片)¿参数有什么问题,为什么不保存通货膨胀?感谢
活动模型
cla
has_and_belongs_to_many :users
has_and_belongs_to_many :sponsors
has_and_belongs_to_many :category
has_and_belongs_to_many :places
accepts_nested_attributes_for :places
end
放置模型
has_and_belongs_to_many :events
has_one :scores
has_and_belongs_to_many :categorys
accepts_nested_attributes_for :events
事件控制器
class EventsController < ApplicationController
before_action :events_params, only: [:create, :edit]
def index
@event = Event.all
end
def show
@event = Event.find(get_event)
end
def new
@event = Event.new
end
def create
@event = Event.new(events_params)
if @event.save
redirect_to events_path, notice: "Almacenado"
else
render "new", notice: "Danm"
end
end
def edit
params_filtered = Event.find(events_params)
@event = Event.find(get_event)
if @event.update(params_filtered)
redirect_to events_path
else
render 'new'
end
end
def destroy
@event = Event.find(get_event)
@event.destroy
redirect_to events_path
end
private
def events_params
params.require(:event).permit(:website,:city,:whybuy,:moendreinfo,:title,:description, :datestart, :dateend, :hourstart, :hourend, :price ,:avatar, :background, places_atributtes: [])
end
def get_event
@event = Event.find(params[:id])
end
end
路线
Rails.application.routes.draw do
root 'welcome#index'
resources :places do
resources :events
end
resources :events
get "events/:id" => "events#show"
get "events/:id/edit" => "events#edit"
devise_for :users, :controllers => { registrations: 'registrations' }
devise_scope :users do
get "profile_user/:id" => "users_profiles#profile_user"
get "usersall" => "users_profiles#usersall"
end
end
错误错误链接
i.stack.imgur.com/Snl2m.png
表格活动新
<html>
<head>
<title></title>
</head>
<body id="newevent">
<h3> Crear Evento </h3>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 ">
<di>
<div id="divnewevent" >
<%=form_for @event do |eve|%>
<%= eve.label :title, "Titulo del evento" %></br>
<%= eve.text_field :title, class:"form-control", placeholder:"Stingi Fest Back Color", autofocus: true%></br>
<%= eve.label :description, "Descripción" %></br>
<%= eve.text_field :description, class:"form-control", placeholder:"No debe superar los 140 caracteres", autofocus: true%></br>
<%= eve.collection_select(:place_ids, Place.all, :id, :name, {prompt: true}, {multiple: true, class: "input-lg"}) %>
<%= eve.label :datestart, "Fecha de apertura" %></br>
<%= eve.date_select :datestart, class:"form-control", placeholder:"Fecha de apertura o día del evento", autofocus: true %></br>
<%= eve.label :dateend, "Fecha de cierre" %></br>
<%= eve.date_select :dateend, class:"form-control", placeholder:"Fecha de cierre o día del evento", autofocus: true %></br>
<%= eve.label :hourstart, "Hora inicio" %></br>
<%= eve.time_select :hourstart, class:"form-control", placeholder:"Hora de inicio", autofocus: true %> </br>
<%= eve.label :hourend, "Hora cierre" %></br>
<%= eve.time_select :hourend, class:"form-control", placeholder:"Hora de cierre", autofocus: true %></br>
<%= eve.label :website, "website" %></br>
<%= eve.text_field :website, class:"form-control", placeholder:"www.cosito.com", autofocus: true %></br>
<%= eve.label :city, "ciudad" %></br>
<%= eve.text_field :city, class:"form-control", placeholder:"Ciudad,co", autofocus: true %></br>
<%= eve.label :price, "Costo" %>
<%= eve.text_field :price, class:"form-control", placeholder:"Costo de entrada", autofocus: true %> </br>
<%= eve.label :whybuy, "Por qué asistir o comprar y dónde" %></br>
<%= eve.text_field :whybuy, class:"form-control", placeholder:"Plus de insentivo y donde comprar", autofocus: true %></br>
<%= eve.label :moreinfo, "más info" %></br>
<%= eve.text_field :moreinfo, class:"form-control", placeholder:"Mayor información", autofocus: true %></br>
<%= eve.label :avatar, "Foto de perfil" %>
<%= eve.file_field :avatar %>
<%= eve.label :background, "Foto de fondo" %>
<%= eve.file_field :background %>
<%= eve.submit %>
<%end%>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
我的评论太低了。
请显示表格的视图。看起来好像这些参数不符合您的
params.require(:event)
编辑:
我认为你没有以正确的方式使用你的event_params
,只有当你想检查给定的参数并寻找你允许的属性时才应该调用这个函数。
你应该删除
before_action :event_params
但是补充说:
before_action :get_event, except: [:new, :index, :create]
这将确保您在show, update, destroy
之前的@ event-variable中设置当前事件。
我认为您的罪魁祸首可能是您没有update
- 方法,您也可以在edit
- 方法中执行此操作。
edit
是加载您可以编辑活动的页面的操作。
update
是您从表单发送的操作。您可以在此处使用@event.update(...)
。
因此,edit
和update
可能如下所示:
def edit
end
def update
if @event.update(event_params)
redirect_to @event, notice: 'Event was updated'
else
render action: :edit
end
end
(如果您添加了before_action :get_event
)。