【发布时间】: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