gpt4 book ai didi

ruby-on-rails - 没有路由匹配 [GET] "/simulations"

转载 作者:数据小太阳 更新时间:2023-10-29 08:42:21 31 4
gpt4 key购买 nike

我对这个论坛和一般的编码真的很陌生:)

我正在尝试在我的 ruby​​ on rails 应用程序中创建一个小模拟器。我创建了一个模型,它采用所有属性来进行计算,模拟的最终结果将存储在我的用户模型的一个属性中。模拟器的目标:用户,一个自由职业者,输入他所有的企业信息,并在模拟结束时知道他要缴纳多少税。我现在在我的模拟类的“set_result”方法中编码了一个可能的路径。

模拟的所有属性都通过 View 中的一个简单的 form_for 询问用户,然后根据用户对表单的输入计算模拟的结果。我的模型看起来像这样:

class Simulation < ApplicationRecord
belongs_to :user

ACTIVITES = ["liberale", "commerciale ou industrielle", "artisanale"]
YEAREXISTENCE = [1, 2, 3, 4]

before_save :set_result

validates :activity, presence: true, inclusion: { in: ACTIVITES }
validates :year_existence, presence: true, inclusion: { in: YEAREXISTENCE }
validates :reglementary, presence: true
validates :accre, presence: true

def set_result
answer = params[:simulation]

if answer[:activity] == "liberale" && answer[:year_existence] == 2 && answer[:accre]
self.result = 0.12
else
self.result = 0.23
end
end
end

我的 Controller :

class SimulationsController < ApplicationController
before_action :set_simulation, only: [:show, :edit, :update, :destroy, :set_tax_rate]

def new
@simulation = Simulation.new
end

def create
binding.pry
@simulation = Simulation.new(simulation_params)
if @simulation.save
render :show
else
render :new
end
end

private

def set_simulation
@simulation = Simulation.find(params[:id])
end

def simulation_params
params.require(:simulation).permit(:activity, :user_id)
end
end

我的观点是我把 simple_form_for 放在哪里,并询问模拟结果所需的所有属性。

<div class="container">
<h1 class= "structure ml-5">simulation</h1>
</div>

<!-- <h1>votre tx : <%= @result %></h1> -->

<div class="row">
<div class="col-5">
<%= simple_form_for @simulation, root_path, method: :get do |f| %>

<%= f.input :activity,
label:"Quelle est votre catégorie d'activité ?",
collection: Simulation::ACTIVITES,
include_blank: true,
as: :select %>

<%= f.input :reglementary,
label:"Est-ce une activité libérale règlementée ?",
as: :boolean,
include_blank: true,
as: :select %>

<%= f.input :year_existence,
label:"Quel est l'âge de l'entreprise en année d'activité ?",
collection: Simulation::YEAREXISTENCE,
include_blank: true,
as: :select %>

<%= f.input :accre,
label:"Avez-vous obtenu l'ACCRE lors de votre création d'entreprise ?",
as: :boolean,
include_blank: true,
as: :select %>

<%= f.submit "valider", class: "btn btn-primary" %>
<% end %>
</div>
</div>
``````````````

I tried to "binding.pry" to see if the form gets me to the create action from the controller but it seems that it does not.
When I get to the route : "/simulations/new", i can enter infos, but when i submit I have the error : "No route matches [GET] "/simulations"". As is I was redirecting my user to the "index" of simulations, which I don't think I did in my code...

when I try to create a new simulation in my console by for example

simul = Simulation.new(activity: "liberale"...)
simul.user_id = User.last

and then, trying to create it in my db :

simul.save


I get this error :

"NameError: undefined local variable or method `params' for #<Simulation:0x00007fcb6ea6f5b8>
from /Users/mac/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activemodel-5.2.3/lib/active_model/attribute_methods.rb:430:in `method_missing"

of course, I removed all the raise and binding.pry before trying it :)
anyone has an idea of what gets in the way of my simulation params ???
I am not sure of the "set_result" method and especially how I called the params for simulation... maybe that's the problem. I tried to see in my previous apps, and seems that I did the same though...

最佳答案

删除 simple_form 调用中的“url”和“method”选项。如下使用

<%= simple_form_for @simulation do |f| %>

默认情况下,simple_forms 将使用“POST”http 方法提交表单。在错误中,我可以看到它由于“GET”http 方法而命中模拟/索引端点。

/simulations 的“POST”将触发 Controller 中的创建操作。希望它有效。

关于ruby-on-rails - 没有路由匹配 [GET] "/simulations",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57738834/

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