【问题标题】:Undefined method which is not used未使用的未定义方法
【发布时间】:2014-07-27 07:59:37
【问题描述】:

您好,我是 ruby​​ on rails 的新手。我有奇怪的问题。我使用了一些教程并得到了我不应该得到的错误。

我有控制器

class DiaryController < ApplicationController

before_action :authenticate_user!

respond_to :html, :xml, :json
respond_to :js, :only => [:create, :update, :destroy]

def create
@record = Record.create(record_params)
@record.userId=current_user.id

if request.xhr? || remotipart_submitted?
  sleep 1 if params[:pause]
  render :layout => false, :template => (params[:template] == 'escape' ? 'comments/escape_test' : 'diary/create'), :status => (@record.errors.any? ? :unprocessable_entity : :ok)
else
  redirect_to diary_path
end
end

def add
@record = Record.new
#respond_with(@record, :layout => false)
respond_with do |format|
  format.html { render :layout => ! request.xhr? }
end
end

# PUT /comments/1
# PUT /comments/1.xml
def update
  @record = Record.find(params[:id])
  respond_with do |format|
  format.html{ redirect_to @record }
end
end

def delete
  @comment = Comment.destroy(params[:id])
end

def edit
  @record = Record.find(params[:id])
end

def index
  @records = Record.where(userId: current_user.id)
end

private

def record_params
  params.require(:record).permit(:photo, :comment, :date, :photo_cache)
end

结束

有观点

<h1 align="centre">
  Добавить запись
</h1>

<%= render 'form' %>

<%= link_to 'Отмена', diary_path, :id => 'cancel-button' %>

<%= form_for(@record, :remote => (params[:action] == 'add' ? true : false)) do |f| %>

<fieldset>
  <div class="field">
    <%= f.label :date, :class => 'required' %><br />
    <%= f.date_select :date %>
  </div>

  <div class="field">
    <%= f.label :comment %><br />
    <%= f.text_area :comment %>
  </div>

  <div class="field">
    <%= image_tag(@record.photo_url(:thumb)) if @record.photo? %><br />
    <%= f.label :photo %><br/>
    <%= f.file_field :photo %><br/>
    <%= f.hidden_field :photo_cache %>
  </div>

 </fieldset>

<table>
  <tr>
    <td>
      <div class="actions">
        <%= f.submit "Добавить", :data => {:'disable-with' => "Submitting..."} %>
      </div>
    </td>
    <td>
      <%= link_to 'Отмена', diary_path, :id => 'cancel-button' %>
    </td>
  </tr>
</table>

并得到 ActionView::Template::Error (undefined method `records_path' for #): error on " (params[:action] == 'add' ? true : false)) 做 |f| %>" 行。甚至我没用过的 records_path。

我有路线

devise_for :users
get 'welcome/index'
root 'welcome#index'
get 'diary' => 'diary#index'
get 'diary/add_record', to: 'diary#add', as: 'add_record'
post 'diary/add_record', to: 'diary#create'
get 'diary/edit_record/:id', to: 'diary#edit'
delete 'diary/edit_record/:id' => 'diary#delete

并尝试使用 add_record 路由。也许使用资源会更好:记录。但我想弄清楚为什么我的路线不起作用。

查看名称“日记”。

【问题讨论】:

  • 你想使用什么路径...?我看到你正在使用@record = Record.new,所以 Rails 会自动将它映射到记录控制器创建操作。除非您在表单中添加自定义网址。
  • 你的视图文件名是什么?
  • 我编辑我的问题以澄清您的问题
  • 你的视图名称是diary.html.erb吗?

标签: ruby-on-rails


【解决方案1】:

因为您是 RoR 的新手,所以让我解释一下您收到错误的原因


form_for

form_for 是您收到此错误的可能原因(哦,我刚刚看到它实际上表明这是发生错误的地方 - 甜蜜)

您遇到的问题是form_for 是一种将表单围绕 ActiveRecord 对象呈现的方式。这意味着通过在newcreate 操作中使用AR 对数据进行一些半持久性(允许您在提交后在表单上显示输入的数据)

当您将对象传递给 form_for 时,Rails 会自动从 ActiveRecord 对象“构建”表单,它使用的选项之一是 url

--

路线

您遇到的问题是您传递给form_for 的对象采用model_name 属性来构建路由。这意味着如果您想通过传递一个对象来使用form_for 方法,它将寻找直接与该对象相关的路由

如果您没有设置任何 [model]_path 路由,您可能会收到您遇到的错误。修复首先涉及路由,其次涉及控制器

#config/routes.rb
root 'welcome#index'
devise_for :users

resources :diary, path_names: { new: "add_record", create: "add_record", edit: "edit_record", destroy: "edit_record" }
resources :welcome, only: :index

这归结于 Rails 的路由结构是built around resources - 你拥有的每条路由都应该导致特定的控制器操作。虽然包含自定义操作完全没问题,但您必须了解路由结构的基础是构造resourceful 路由,这实质上意味着Rails 感知每个控制器/模型都有相应的路由:

--

网址

要观察的第二件事是表单的url

如果您的路由设置如上,并且您的路由结构与模型结构不同(不同的名称),您需要使用以下设置明确定义url

<%= form_for @record, url: your_custom_path do |f| %>

【讨论】:

    猜你喜欢
    • 2018-12-19
    • 1970-01-01
    • 2017-08-03
    • 2019-06-24
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多