具有简单形式的未定义方法路径 [英] undefined method path with simple form

查看:59
本文介绍了具有简单形式的未定义方法路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相册pictures,其中相册有很多图片

这是我的routes.rb

 资源:专辑做资源:照片结尾

我的路径是 album_photos_path 而不是 photos_pathmy photos/new.html.erb 中,我收到此错误:

未定义的方法photos_path' for #<#:0x3cd55a0>

我该怎么做来代替 photos_path 简单的形式写 album_photos_path ?

我的 new.html.erb

<%= simple_form_for([@album, @photo]) do |f|%><%= f.error_notification %><div class="form-inputs"><%= f.input :title %><%= f.input :description %>

<div class="form-actions"><%= f.button :submit %>

<%结束%>

解决方案

您可以指定 url 在您的表单中.像这样:

<%= simple_form_for @photo, url: album_photos_path do |f|%><%= f.error_notification %><div class="form-inputs"><%= f.input :title %><%= f.input :description %>

<div class="form-actions"><%= f.button :submit %>

<%结束%>

但是您的代码也应该可以工作.在您的新操作中,您是否同时初始化了@album 和@photo,例如:

def new@album = Album.find params[:album_id]@photo = @album.pictures.build结尾

P.S 以上代码只是示例代码,如果两个变量(@album 和 @photo)都正确初始化,那么 rails 将自动生成正确的 url.

I have albums and pictures where albums hasmany pictures

This is my routes.rb

 resources :albums do
  resources :photos
end

Instead of photos_path my path is album_photos_path In my photos/new.html.erb I'm getting this error:

undefined method photos_path' for #<#<Class:0x5232b40>:0x3cd55a0>

How I can do to instead of photos_path simple form write album_photos_path ?

My new.html.erb

<%= simple_form_for([@album, @photo]) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
</div>

<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>

解决方案

You can specify url in your form. Like this:

<%= simple_form_for @photo, url: album_photos_path do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :title %>
    <%= f.input :description %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

But your code should also work. In your new action did you initialize both @album and @photo, something like:

def new
  @album = Album.find params[:album_id]
  @photo = @album.pictures.build
end

P.S above code is just a sample code and if both variables(@album and @photo) are initialized properly then rails will automatically generate correct url.

这篇关于具有简单形式的未定义方法路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆