gpt4 book ai didi

ruby-on-rails - 除非我刷新页面 form_for Rails,否则提交按钮不起作用

转载 作者:行者123 更新时间:2023-12-04 01:31:59 25 4
gpt4 key购买 nike

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

这是我的 Controller

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>

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

最佳答案

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

<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>

关于ruby-on-rails - 除非我刷新页面 form_for Rails,否则提交按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23339504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com