我目前正在使用通过关联具有has_many
的嵌套资源。
试图简单地编辑我的dynamic_elements值,但是在生成编辑页面时,将使用表单按钮启动表单以完成发布操作。我读过一点Rails可以做到这一点,因为某些浏览器不支持PUT,因此只发布了Post。我也读过,以防止表格重复,我只需要将:id
放在强参数中即可。唯一的问题是,:id
在我提交表单时没有将其纳入参数。 :id
出现在用于编辑视图的参数中,但是一旦我单击“提交”,就找不到它们了。这是一些代码:
在这里,您可以看到{"dynamic_page_id"=>"2", "id"=>"31"}
的参数存在于编辑方法/视图中,但是一旦调用更新方法,"id"=>"31"
就会丢失
路线:
namespace :admin do
resources :dynamic_pages do
resources :dynamic_elements
end
end
耙道: enter image description here
型号:
module Admin
class DynamicElement < ApplicationRecord
has_many :dynamic_page_elements
has_many :dynamic_pages, :through => :dynamic_page_elements
end
end
module Admin
class DynamicPage < ApplicationRecord
has_many :dynamic_page_elements
has_many :dynamic_elements, :through => :dynamic_page_elements
end
end
module Admin
class DynamicPageElement < ApplicationRecord
belongs_to :market_page
belongs_to :market_element
end
end
控制器:
module Admin
class DynamicElementsController < ApplicationController
before_action :set_dynamic_element, only: [:show, :edit, :update, :destroy]
before_action :set_dynamic_page, only: [:index, :show, :edit, :update, :destroy]
def index
@dynamic_elements = DynamicElement.all
# @dynamic_page = DynamicPageElement.find(params[:dynamic_page_id])
end
def show
end
def new
@dynamic_element = DynamicElement.new
end
def edit
@dynamic_element = DynamicElement.find(params[:id])
@styles_hash = @dynamic_element.style
end
def create
# @dynamic_element = DynamicElement.find(params[:id])
@dynamic_element = DynamicElement.new(dynamic_element_params)
respond_to do |format|
if @dynamic_element.save
format.html { redirect_to admin_dynamic_page_dynamic_elements_url, notice: 'Dynamic element was successfully created.' }
format.json { render :show, status: :created, location: @dynamic_element }
else
format.html { render :new }
format.json { render json: @dynamic_element.errors, status: :unprocessable_entity }
end
end
@dynamic_page_element = DynamicPageElement.new(dynamic_element_id: @dynamic_element.id, dynamic_page_id: params[:dynamic_page_id])
@dynamic_page_element.save
end
def update
respond_to do |format|
if @dynamic_element.update(dynamic_element_params)
format.html { redirect_to admin_dynamic_page_dynamic_elements_path, notice: 'Dynamic element was successfully updated.' }
format.json { render :show, status: :ok, location: @dynamic_element }
else
format.html { render :edit }
format.json { render json: @dynamic_element.errors, status: :unprocessable_entity }
end
end
end
def destroy
@dynamic_page_element = DynamicPageElement.where(dynamic_element_id: params[:id])[0]
@dynamic_page_element.destroy
@dynamic_element.destroy
respond_to do |format|
format.html { redirect_to admin_dynamic_page_dynamic_elements_path, notice: 'Dynamic element was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_dynamic_element
@dynamic_element = DynamicElement.find(params[:id])
end
def set_dynamic_page
@dynamic_page = DynamicPage.find(params[:dynamic_page_id])
end
def dynamic_element_params
params.permit(:style, :description, :id)
end
end
end
表格:
<%= form_with(url:admin_dynamic_page_dynamic_elements_url) do |form| %>
<% if dynamic_element.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(dynamic_element.errors.count, "error") %>
prohibited this dynamic_element from being saved:</h2>
<ul>
<% dynamic_element.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :description %>
<%= form.text_field :description, value: @dynamic_element.description %>
</div>
<div class="field">
<%= form.label :style %>
<%= form.text_area :style, value: @styles_hash %>
</div>
<h5>Current Styles</h5>
<ul style="list-style-type: none;">
<% @dynamic_element.style.keys.zip(@dynamic_element.style.values).each do |key, value| %>
<li>"<%= key %>" => "<%= value %>", </li>
<% end %>
</ul>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
我尝试了url:admin_dynamic_page_dynamic_elements_url
而不是我的表单助手中的model: @market_page
,但只是抛出了此错误:
undefined method `admin_dynamic_element_path' for #<#<Class:0x007ffe56cf57b8>:0x007ffe56c45340>
Did you mean? admin_dynamic_page_path
admin_dynamic_pages_path
我似乎无法弄清楚,任何帮助将不胜感激。如果我需要提供更多信息,请告诉我!谢谢!