【问题标题】:Issue with a nested form_for's parameter嵌套 form_for 参数的问题
【发布时间】:2016-12-17 16:38:58
【问题描述】:

我正在创建一个人们可以阅读漫画的网站,这要归功于三个支架:一个用于漫画本身,一个用于章节,最后一个用于章节页面。在我的 routes.rb 文件中,我将页面嵌套在章节资源中,我嵌套在漫画资源中,所以我的路线如下所示:

  resources :mangas do
    resources :chapters do
      resources :pejis #Rails doesn't like the "scan" word
    end
  end

我可以毫无困难地创作漫画,但由于未知原因,我无法使表格出现:

views/chapters/_form.html.erb

<%= form_for(chapter) do |f| %>

  <div class="field">
    <%= f.label :titre %>
    <%= f.text_field :titre %>
  </div>

  <div class="field">
    <%= f.label :apercu %>
    <%= f.file_field :apercu %>
  </div>

  <div class="field">
    <!-- Allows me to upload the chapter's pages in the same time -->
    <label for="images[]">Multi Upload</label>
    <%= file_field_tag 'images[]', type: :file, multiple: true %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

form_for 参数与脚手架命令创建时一样。但是,显然,它不起作用,因为我将资源嵌套在漫画脚手架内。我尝试了一些东西,直到new_manga_chapter_path 允许我看到表格。但是,在提交时,我收到以下错误: No route matches [POST] "/mangas/1/chapters/new"。我什至不确定这是正常还是奇怪。

我很确定我不应该将“new_manga_chapter_path”作为form_for 的参数,但我不知道该放什么。

以防万一,这里是章节控制器:

class ChaptersController < ApplicationController
  before_action :set_chapter, only: [:show, :edit, :update, :destroy]

  def index
    @chapters = Chapter.all
  end

  def show
  end

  def new
    @chapter = Chapter.new

  end

  def edit
  end

  def create
    @chapter = Chapter.new(chapter_params)

    if @chapter.save
      (params[:images] || []).each_with_index do |image, index|
        @chapter.pejis.create(image: image, scan_number: index + 1)
      end
      redirect_to @chapter, notice: 'Chapter was successfully created.'
    else
      render :new
    end
  end

  def update
    if @chapter.update(chapter_params)
      (params[:images] || []).each_with_index do |image, index|
        @chapter.pejis.create(image: image, scan_number: index + 1)
      end
      redirect_to @chapter, notice: 'Chapter was successfully updated.'
    else
      render :edit
    end
  end

  def destroy
    @chapter.destroy
    redirect_to chapters_url, notice: 'Chapter was successfully destroyed.'
  end

  private

    def set_chapter
      @chapter = Chapter.friendly.find(params[:id])
    end

    def chapter_params
      params.require(:chapter).permit(:titre, :apercu)
    end
end

如果你想看其他东西,不要犹豫

提前谢谢你

【问题讨论】:

    标签: ruby-on-rails forms nested


    【解决方案1】:

    试试这个:

    # don't replace the [] by ()
    <%= form_for [@manga, @chapter] do |f| %>
    

    或者

    <%= form_for manga_chapter_path(@manga, @chapter) %>
    

    在你的控制器中:

    def new
      @manga = Manga.find(params[:manga_id])
      @chapter = Chapter.new
    end
    

    在您的 cmets 之后为您提供帮助: 在您的创建方法中:

    def create
        @manga = Manga.find(params[:manga_id])
        @chapter = Chapter.new(chapter_params)
    
        if @chapter.save
          (params[:images] || []).each_with_index do |image, index|
            @chapter.pejis.create(image: image, scan_number: index + 1)
          end
          redirect_to manga_chapter_path(@manga, @chapter), notice: 'Chapter was successfully created.'
        else
          render :new
        end
      end
    

    【讨论】:

    • 想想我自己考虑过这个anwser,但无法设置它。非常感谢!!
    • 但是我有一个最后一个(小)问题:一切正常,但是一旦创建该章我就无法访问该章(它在“重定向到”时失败)。我尝试使用 manga_chapter_path(id:@chapter.id,manga_id:@manga),但即使我将它添加到 set_chapter 函数中,它也找不到@manga。有什么想法吗?
    • 错误是什么?您必须在方法末尾添加(可能创建)“redirect_to manga_path(@manga)”
    • 是的,但我想重定向到章节#show,我无法在“创建”操作后设置它
    • 替代方案是:redirect_to "/mangas/#{@manga.id}/chapters/#{@chapter.id}"
    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 2012-07-08
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多