【发布时间】:2016-03-05 15:17:25
【问题描述】:
我有一个适用于应用程序中所有表单的模板,但是,我想在不同的操作中为表单的提交按钮定义不同的名称(例如,当我编辑一篇文章时,我希望从提交按钮显示文本 更新文章,当我添加文章时,我希望从提交按钮显示文本添加文章)。有没有办法做到这一点,但保持呈现相同的表单模板?
<%= form_for @article do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@article.errors.count, "error") %>
prohibited this article from saving
</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
这是 ArticlesController:
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "username", password: "pass", except: [:index, :show]
def index
@article = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
【问题讨论】:
-
你能显示文章控制器吗?
-
@RahulSingh 我发布了。
标签: ruby-on-rails forms