Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 文章中的Ruby on Rails命名错误#show_Ruby On Rails - Fatal编程技术网

Ruby on rails 文章中的Ruby on Rails命名错误#show

Ruby on rails 文章中的Ruby on Rails命名错误#show,ruby-on-rails,Ruby On Rails,我一直得到一个无方法错误。为什么?我怎样才能解决这个问题 文章中的命名错误#显示 未定义的方法“photo”# 我正在使用ruby on rails,我正在尝试使用回形针,这样我就可以在我的应用程序上上传照片 我节目文件的一部分 <%= render @article.photos %> #source of error <h3>Add a photo:</h3> <%= render 'photos/form' %> #错误源 添加照片:

我一直得到一个无方法错误。为什么?我怎样才能解决这个问题

文章中的命名错误#显示 未定义的方法“photo”#

我正在使用ruby on rails,我正在尝试使用回形针,这样我就可以在我的应用程序上上传照片

我节目文件的一部分

<%= render @article.photos %>  #source of error
<h3>Add a photo:</h3>
<%= render 'photos/form' %>
#错误源
添加照片:
我的照片控制器

class PhotosController < ApplicationController

 #Index action, photos gets listed in the order at which they were created
 def index
  @photos = Photo.order('created_at')
 end

 #New action for creating a new photo
 def new
  @photo = Photo.new
 end

 #Create action ensures that submitted photo gets created if it meets the requirements
 def create
  @article = Article.find(params[:article_id])
  @photo = @article.photos.create(photo_params)
 redirect_to article_path(@article)


 end

 def destroy
        @article = Article.find(params[:article_id])
        @photo = @article.photos.find(params[:id])
        @photo.destroy
        redirect_to article_path(@article)
 end

 private

 #Permitted parameters when creating a photo. This is used for security reasons.
 def photo_params
  params.require(:photo).permit(:title, :image)
 end

end
class ArticlesController < ApplicationController
   def new
          @article = Article.new
    end

    def index
     @articles = Article.all
    end

    def show
        @article = Article.find(params[:id])
    end

    def create
         @article = Article.new(article_params)

        @article.save
        redirect_to @article
    end

    def edit
        @article = Article.find(params[:id])
    end

    def update
        @article = Article.find(params[:id])
        if @article.update(article_params)
        redirect_to @article
        else
            render 'edit'
        end

    end

    def destroy
        @article = Article.find(params[:id])
        @article.destroy

        redirect_to articles_path
    end
end

private
def article_params
    params.require(:article).permit(:title, :text)
end
class photocontroller
==========更新=======

这是我的 物品管理员

class PhotosController < ApplicationController

 #Index action, photos gets listed in the order at which they were created
 def index
  @photos = Photo.order('created_at')
 end

 #New action for creating a new photo
 def new
  @photo = Photo.new
 end

 #Create action ensures that submitted photo gets created if it meets the requirements
 def create
  @article = Article.find(params[:article_id])
  @photo = @article.photos.create(photo_params)
 redirect_to article_path(@article)


 end

 def destroy
        @article = Article.find(params[:article_id])
        @photo = @article.photos.find(params[:id])
        @photo.destroy
        redirect_to article_path(@article)
 end

 private

 #Permitted parameters when creating a photo. This is used for security reasons.
 def photo_params
  params.require(:photo).permit(:title, :image)
 end

end
class ArticlesController < ApplicationController
   def new
          @article = Article.new
    end

    def index
     @articles = Article.all
    end

    def show
        @article = Article.find(params[:id])
    end

    def create
         @article = Article.new(article_params)

        @article.save
        redirect_to @article
    end

    def edit
        @article = Article.find(params[:id])
    end

    def update
        @article = Article.find(params[:id])
        if @article.update(article_params)
        redirect_to @article
        else
            render 'edit'
        end

    end

    def destroy
        @article = Article.find(params[:id])
        @article.destroy

        redirect_to articles_path
    end
end

private
def article_params
    params.require(:article).permit(:title, :text)
end
class-ArticlesController
文章模式

class Article < ApplicationRecord
    has_many :comments
end
类文章
我现在修复了它,但现在我有另一个无方法错误

未定义的方法“article\u photos\u path”# 你是说?第11条道路

<%= form_for([@article, @article.photos.build]) do |f| %> #source of error
  <div class="form-group">
     <%= f.label :image %>
    <%= f.file_field :image, class: 'form-control'%>
 </div>
    <p>
        <%= f.submit 'Upload Photo' %>
    </p>
    <% end %>
    </p>
    <% end %>
#错误源


正在拍摄另一个模型,因此,您需要建立正确的关系:

class Article < ApplicationRecord
  has_many :comments
  has_many :photos
end

class Photo < ApplicationRecord
  belongs_to :article
end
之后,您应该更新它们:

params.require(:photo).permit(:title, :image, :article_id)

文章控制器中有什么?刚刚更新了问题“文章-照片关系是如何定义的?”?还需要查看文章模型。您还必须向config/routes.rb添加适当的路由,如下所示:
resources:articles member do get:photos end