我有一个模板用于应用中的所有表单,但是,我想为不同的操作定义不同的表单提交按钮名称(例如,当我编辑文章I想从提交按钮显示文字更新文章,当我添加一篇文章,我想从提交按钮显示文字添加文章)。有没有办法做到这一点,但保持呈现相同的表单模板?在表单中提交不同名称的提交按钮
<%= 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我发布了它。 – Nikola