【问题标题】:submit action doesnt work in Rails 4提交动作在 Rails 4 中不起作用
【发布时间】:2014-09-25 09:29:35
【问题描述】:

当我提交表单时,新数据没有保存在数据库中,因此我看不到它们,但是当我创建一个新数据时,例如 welcome= Welcome.new 和 welcome.title="your are"通过 rails 控制台他们可以工作,我只能看到我在控制台中所做的事情,而不是通过应用程序。看来提交底部没有保存任何数据。

new.html.erb

    <h1> Submite </h1>

  <form action="new" method="post" class="form-horizontal" role ="form">
   <div class="form-group">
      <label for"kind"> kind </label>
      <input type="string" id="kind" name="kind" class="form-control">
  </div>

    <br>
    <div class="form-group">
      <label for"title"> title </label>
      <input type="string" id="title" name="title" class="form-control">
    </div>
    <br>
    <div class="form-group">
      <label for"text">"text </label>
      <input type="text" id="text" name="text" class="form-control">
      <br>
  </div>
      <br>
  </div>
      <input type="submit" value="Submit" class="btn btn-primary">
  <a href="/welcomes/index">List </a>
  </div>

index.html.erb

        <% @welcomes.each do |welcome| %>
            <li><%= welcome.title %>  </li>
          <% end %>

welcomes_controller

    def index
    @welcomes = Welcome.all
    end

    def new
         @welcome = Welcome.new
    end

    def create
         @welcome = Welcome.new(user_params)    
        if @welcome.save
         flash.keep[:notice]="Successfuly created"
               render :index
        else
        render :new
        #  format.json { render json: @user.errors, status: :unprocessable_entity }
        end

       end

    private

     def user_params
        params.require(:welcome).permit(:kind, :title, :text)
      end

【问题讨论】:

    标签: ruby html ruby-on-rails-4 web


    【解决方案1】:

    您的表单没有提交到正确的路径。改用表单生成器:

    <%= form_for @welcome do |f| %>
       <%= f.label :kind %>
       <%= f.text_field :kind %>
    
       <%= f.submit  %>
    <% end %>
    

    这会将表单提交到您的 HTML 中的正确路径

    【讨论】:

      猜你喜欢
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多