这是我的型号代码。
class Subject < ActiveRecord::Base
has_many :pages
validates :name,
:presence => true,
:length => { :maximum => 255 }
scope :visible, lambda { where(:visible => true) }
scope :invisible, lambda { where(:visible => false) }
scope :sorted, lambda { order("subjects.position ASC")}
scope :newest_first, lambda { order("subject.created_at DESC") }
scope :search, lambda { |query|
where(["name LIKE ?", "%#{query}%"])
}
end
我注意到它说的是我的创作动作。所以这就是:
class SubjectsController < ApplicationController
layout "admin"
def index
@subjects = Subject.sorted
end
def show
@subject = Subject.find(params[:id])
end
def new
@subject = Subject.new({:name => 'Default'})
@subject_count = Subject.count + 1
end
def create
# Instantiate a new object using form parameters
@subject = Subject.new(subject_params)
# Save the object
if @subject.save
# If save succeeds, redirect to the index
flash[:notice] = "The subject was created successfully."
redirect_to(:action => 'index')
else
@subject_count = Subject.count + 1
render(new)
end
end
def edit
@subject = Subject.find(params[:id])
@subject_count = Subject.count
end
def update
# Find an existing object using form parameters
@subject = Subject.find(params[:id])
# Update the object
if @subject.update_attributes(subject_params)
# If save succeeds, redirect to the show
flash[:notice] = "The subject was updated successfully."
redirect_to(:action => 'show', :id => @subject.id)
else
# If update fails, redisplay the form so user can fix problem.
@subject_count = Subject.count
render('edit')
end
end
def delete
@subject = Subject.find(params[:id])
end
def destroy
subject = Subject.find(params[:id]).destroy
flash[:notice] = "The subject '#{subject.name}' was deleted successfully."
redirect_to(:action => 'index')
end
private
def subject_params
# same as using "params[:subject]", except that it:
# -raises an error if :subject is not present
# -allows listed attributes to be mass-assigned
params.require(:subject).permit(:name, :position, :visible)
end
end
如果我取消验证它可以工作,但我可以创建没有名称的主题。我注意到,如果我添加一个主题,副手'3'中的数字会向上移动。我想知道它是否与选择表格有关。这是表格:
<%= error_messages_for(@subject) %>
<table summary="Subject form fields" class="table table-hover">
<tr>
<th><%= f.label(:name, "Name") %></th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th><%= f.label(:position) %></th>
<td><%= f.select(:position, 1..@subject_count) %></td>
</tr>
<tr>
<th><%= f.label(:visible) %></th>
<td><%= f.check_box(:visible) %></td>
</tr>
</table>
更新:我正在为每个请求添加索引视图。
<% @page_title = 'Subjects' %>
<div class="subjects show">
<h2>Subjects</h2>
<%= link_to("Add New Subject", {:action => 'new'}, :class => 'action new btn btn-default') %>
<table class="listing table table-hover" summary="Subject list">
<tr class="header">
<th>Position</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% @subjects.each do |subject| %>
<tr>
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
<td class="center"><%= subject.pages.size %></td>
<td class="actions">
<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
<%= link_to("Edit", {:action => 'edit', :id => subject.id}, :class => 'action edit') %>
<%= link_to("Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
</div>
答案 0 :(得分:1)
由于else
操作的create
部分的部分语法错误,活动模型抛出异常(http://apidock.com/rails/v3.2.1/ActionView/PartialRenderer/partial_path)
def create
#few lines escaped....
else
@subject_count = Subject.count + 1
render(new) # <-- Error
end
Nobilik提到的render :new
或render 'new'
应该是def create
#few lines escaped....
else
@subject_count = Subject.count + 1
render :new # for best practice
end
或NSLocale.preferredLanguages()[0] as String
。你的方法应该是这样的:
NSBundle.mainBundle().preferredLocalizations[0] as NSString