ruby-on-rails - 提交按钮文本来自哪里(Ruby on rails)?

标签 ruby-on-rails forms button submit

我在这里使用 ruby​​ on rails 指南
http://guides.rubyonrails.org/getting_started.html

在第 5.13 节中:
我在提交按钮上显示两个不同的文本值,但在“_form”部分文件中,代码完全相同。 Rails 似乎以某种方式自动更改文本值。在两个 View 中实现这一点的代码在哪里:new.html.erb 和 edit.html.erb。

(我的问题是手动控制文本,而是,我试图了解这种自动行为在 Rails 中的来源。)

_部分的

<%= form_for @post do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
  this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
  <li><%= msg %></li>
<% end %>
</ul>
</div>
 <% end %>
 <p>
  <%= f.label :title %><br>
<%= f.text_field :title %>
</p>

<p>
 <%= f.label :text %><br>
 <%= f.text_area :text %>
</p>

<p>
  <%= f.submit %>
</p>
<% end %>

post_controller
    class PostsController < ApplicationController

def new
    @posts = Post.all
    @post = Post.new
end

def show
    @post = Post.find(params[:id])      
end

def index
    @posts = Post.all
end

# called by the posts_path function
def create
    @post = Post.new(post_params)
    if @post.save
        # Using redirect_to creates a new request.
        redirect_to @post
    else
        # Using render sends back the @post variable's data!
        # i.e. uses same request.
        render 'new'
    end
end

# Can only have one instance of render of redirect_to.
#render text: params[:post].inspect
def update
    @post = Post.find(params[:id])

    if @post.update(params[:post].permit(:title, :text))
        redirect_to @post
    else
        render 'edit'
    end
end

def edit
    @post = Post.find(params[:id])
end

# For SQL injection prevention.
private
    def post_params
        params.require(:post).permit(:title, :text)
    end

结尾

新的.html.erb
最新帖子
      <%= form_for :post, url: posts_path do |f| %> 
<% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited
        this post from being saved:</h2>
      <ul>
            <% @post.errors.full_messages.each do |msg| %>
  <li><%= msg %></li>
            <% end %>
      </ul>
    </div>
    <% end %>

<p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
</p>

<p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
</p>

<p>
    <%= f.submit %>
</p>
  <% end %>

  <%= form_for :post do|f| %>

  <% end %>

  <%= link_to "List of Posts", posts_path %>

编辑.html.erb
编辑帖子
  <%= render 'form' %>

  <%= link_to 'Back', posts_path %>

最佳答案

如果要更改表单提交按钮处的文本:

<%= f.submit ( f.object.new_record? ? "Create" : "Update"), class: "btn" %>

关于ruby-on-rails - 提交按钮文本来自哪里(Ruby on rails)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20768915/

相关文章:

ruby-on-rails - Ruby:找到 10 到 9999 之间的左截断素数(有效)

javascript - 如何在提交时隐藏表单?

javascript - 按钮的背景图像不会缩小

javascript - 为什么运行此脚本后页面会重新加载

javascript - 我的代码不起作用,怎么了?

jquery - 在 IE6、IE7、IE8 中禁用按钮?

ruby-on-rails - Ruby on Rails : prevent from selecting a column by default

mysql - 在 windows 7 x64 上安装 mysql2 时出错,无法正常工作

ruby-on-rails - attr_accessible(*attributes) 和 attr_protected(*attributes) 有什么区别?

javascript错误返回选择html