【问题标题】:Submit button does not work unless I refresh the page form_for Rails除非我刷新页面 form_for Rails,否则提交按钮不起作用
【发布时间】:2014-04-28 11:08:27
【问题描述】:

我有一个有问题模型的应用程序,当我去创建记录时,提交按钮什么也没做。没有错误,它只是不执行,除非我刷新页面并尝试再次添加它。当我去更新记录时也会发生同样的情况。

这是我的控制器

class ProblemsController < ApplicationController
 include Concerns::Votes
def index
 @problems = Problem.all
end

def show
 @problem = find_problem
end

def new
 @problem = Problem.new
end

def edit
 @problem = find_problem
end

def create
 @problem = current_user.problems.new(problem_params)
 @problem.save
 redirect_to @problem
end

def update
 @problem = find_problem
  if @problem.update_attributes(problem_params)
    redirect_to @problem
  else
    redirect_to @problem
  end
end

private

def find_problem
 @problem = Problem.find(params[:id])
end

def problem_params
params.require(:problem).permit(:name, :description, :url)
end 
end

这是我在 new.html 上呈现的 _form.html.erb 部分

<div class="row">
<div class="large-12 columns">
<%= form_for @problem do |f| %>
<label>Name</label>
<%= f.text_field :name, placeholder: "Name your problem!" %>
</div>
 <div class="large-8 columns">
<%= f.text_field :url, placeholder: "Link to any supporting material" %>
 </div>
 <div class="large-12 columns">
 <%= f.text_area :description %>
 </div>
 <div class="large-12 columns">
 <%= f.submit "Create" %>
 </div>
 </div>
 <% end %>

我有资源:我的路线有问题。

这里还有我的 show.html.erb。

<%= div_for @problem do %>
<%= link_to 'Edit', edit_problem_path(@problem) %>
<h2><%= @problem.name %> (<%= @problem.cached_votes_score %>)</h2>
<a =href"<%= @problem.url %>"><%= @problem.url %></a>
<p><%= @problem.description %><p>
By <%= @problem.user.name %></br>
<a class="button"<%= link_to 'Up', {:controller => 'problems', :action => 'up_vote'}, {:method => :post } %></a>
<a class="button"<%= link_to 'Down', {:controller => 'problems', :action => 'down_vote'}, {:method => :post } %></a>
<%= link_to 'Edit', edit_problem_path(@problem) %> |
<%= link_to 'Back', problem_path %>
<% end %>

这是我的 index.html.erb

<div class="row">
<div class="large-12 columns">
  <% @problems.each do |problem| %>
    <h1><small><%= problem.cached_votes_score %></small> <%= link_to problem.name, problem %></h1>
 <% end %>
 </div>
 <%= link_to 'New Problem', new_problem_path %>
</div>

如果我刷新页面,我真的无法理解为什么它会起作用,否则它根本不起作用。

【问题讨论】:

  • form_for 在哪里?
  • @RSB 抱歉,我粘贴了错误的代码。上面我已经修改了。
  • 代替 做 试试这个..
  • 您可能需要更改放置 form_for 和结束标记的位置。只是因为没有放对,所以需要我们刷新页面才能处理函数!

标签: ruby-on-rails button form-for


【解决方案1】:

您的 HTML 无效,提交按钮实际上没有嵌套在表单标签下。尝试将您的视图代码更改为:

<div class="row">
  <div class="large-12 columns">
    <%= form_for @problem do |f| %>
      <label>Name</label>
      <%= f.text_field :name, placeholder: "Name your problem!" %>
      <div class="large-8 columns">
        <%= f.text_field :url, placeholder: "Link to any supporting material" %>
      </div>
      <div class="large-12 columns">
        <%= f.text_area :description %>
      </div>
      <div class="large-12 columns">
        <%= f.submit "Create" %>
      </div>
    <% end %>
  </div>
</div>

【讨论】:

  • form_tag
  • 检查呈现的 HTML 页面的来源。 标签不在
    标签下面。
  • 非常感谢你们俩。对我来说有点暗淡的时刻。
  • 感谢您的回答 - 当我意识到问题是缺少 div 标签的最后一克拉 (&lt;/div) 时,我一直在用头撞墙。奇怪的是,在页面刷新时,Firefox 神奇地为我修复了标记,尽管在初始页面加载时,HTML 被破坏了,很难注意到一个丢失的字符!我什至没有注意到,直到我阅读了这个答案并三次检查提交按钮是否在表单内,并且 Sublime 3 的括号荧光笔插件告诉我它不是!
  • 同样,我有一个额外的 &lt;/div&gt; 并且不知道这会导致我的按钮呈现在我的表单之外
【解决方案2】:

我有同样的问题

之前

<%= simple_form_for(:schedule_list, url: schedulelists_create_with_block_path, :html => { novalidate: false}) do |f| %> 
<div class="row">
  <div class="col-md-12">
    <%= f.button :submit, class: 'pull-right' %>
    <%end%>
  </div>
</div>

之后

<%= simple_form_for(:schedule_list, url: schedulelists_create_with_block_path, :html => { novalidate: false}) do |f| %> 
    <div class="row">
      <div class="col-md-12">
        <%= f.button :submit, class: 'pull-right' %>
      </div>
    </div>
<%end%>

【讨论】:

  • 应该是f.submit 'button-text', class: 'pull-right'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2012-06-19
相关资源
最近更新 更多