嵌套form_for参数的问题

时间:2016-12-17 16:38:59

标签: ruby-on-rails forms nested

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

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

我可以创建一个没有麻烦的漫画,但由于一个未知的原因,我无法设法使表格出现:

视图/章节/ _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参数与scaffold命令创建时的参数相同。但是,显然,由于我在漫画脚手架内嵌套了资源,所以它不起作用。我尝试了一些事情,直到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

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

提前谢谢

1 个答案:

答案 0 :(得分:2)

试试这个:

# 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

在您的评论后帮助您: 在您的create方法中:

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