- 是您的布局逻辑?
/coaches
作为登录的Coach的目的地是有意义的。如果您的应用程序用户只是教练,那么即使是根目录也是合理的。当Coach登录时,他会采用某种形式的coaches#show
,但这并不意味着您只是因为网址类似于/profile
而做错了某些事情。并非所有的东西都需要遵循URL的完整REST.on。在现实世界中,没有任何new
动作驻留在自己的小古董页面上。如果在/coaches
页面上嵌套新的Play表单很有意义,则您的网址无需反映出来。
控制器控制与资源的交互。从/coaches
页面,你仍然可以发送删除播放请求plays#destroy
并通过plays#create
可以render 'coaches/index' when it fails.
我从来没有真正在Rails的处理AJAX验证新的戏剧。
,以确保教练员只能看到自己的剧本,最好的办法是将范围他们通过Coach has_many :plays
关联。 Rails身份验证解决方案(如Devise)中的约定是提供一种方法current_user
,该方法返回当前登录用户的用户模型的实例。
你coaches#index
行动可能看起来像:
# Coaches controller
def index
@plays = current_user.plays
end
那么你的观点可能有:
# views/coaches/index.erb
<ul>
<% for play in @plays %>
<li><%= play.name %></li>
<% end %>
</ul>
你不会做什么:@plays = Play.where(:user_id => @user.id)
。
它还简化并保护其他操作。试想一下:
@play = current_user.plays.new
@play = current_user.plays.build(:name => "My First Play")
redirect_to @play, :notice => "Success!" if @play.save
current_user.plays.find(params[:id]).destroy
要回答添加你的问题作为评论:
# Coaches controller
def new
@play = current_user.plays.new
end
def create
@play = current_user.plays.build(params[:play]) # @play now already contains the association to the coach that created it.
if @play.save!
redirect_to # somewhere
else
render 'coaches/index'
end
end
另一件事我想问问:我怎么有提交到数据库的隐蔽入口 - 如,当有人建立了一个新的游戏,我想这与该用户无需对他们进行相关明确地说。我如何做到这一点? – Red 2011-05-09 18:45:55