Rails 帮助简单形式:未定义方法“model_name”对于哈希:类
我正在尝试使用 gem 简单形式创建一个表单。
该表格应包含所有 konkurrancers 的选择列表。操作 URL 应该是公共控制器和价格比较操作,并带有 konkurrancer 的 ID。我的路线:pricecombare/:id
我的route.rb:
match "/finder/:id" => 'public#pricecompare'
到目前为止我的简单形式:
<%= simple_form_for({:controller => "public", :action => "pricecompare"}, :method => "get") do |f| %>
<%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
<%= f.button :submit, :style => 'display: none;' %>
我收到此错误:
undefined method `model_name' for Hash:Class
我想要的 Html 输出:
<form method="post" id="new_konkurrancer" enctype="multipart/form-data" class="simple_form konkurrancer" action="/public/pricecompare" accept-charset="UTF-8">
<select style=" margin-left:10px;width:370px;float: left;
margin-top: 10px;"name="konkurrancer[form]" id="konkurrancer_form" class="select optional"><option value="">Vælg din A-kasse:</option>
<option value="1">ASE</option>
<option value="2">Træ-industri-byg</option>
<option value="3">Journalistik, kommunikation og sprog</option>
<option value="4">Faglis fælles A-kasse (3F)</option>
<option value="5">Danske lønmodtagere</option>
</select>
<input type="submit" value="Opret konkurrence" name="commit" id="konkurrancer_submit" class="button" style="display: none;">
更新:
我的公共控制器:
def index
@konkurrancer = Konkurrancer
end
def pricecompare
@akasse = Konkurrancer.where(params[:id]).first
@akasserne = Konkurrancer.order(sort_column + " " + sort_direction)
end
<%= simple_form_for(:konkurrancer, :url => {:controller => "finder"}, :method => "get") do |f| %>
<%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
<%= f.button :submit, :style => 'display: none;' %>
<% end %>
我在视图中收到此错误:
ActionController::RoutingError in Public#index
Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised:
No route matches {:controller=>"finder"}
Extracted source (around line #95):
92: <%= render("shared/forside") %>
93: </div>
94: <div style="width:900px;float:left;margin-left:29px;margin-top:15px;"><p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;">Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge</p>
95: <%= simple_form_for(:konkurrancer, :url => {:controller => "finder"}, :method => "get") do |f| %>
ActionController::RoutingError in Public#index
Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised:
No route matches {:controller=>"finder"}
更新新错误日志:
Association cannot be used in forms not associated with an object
Extracted source (around line #96):
93: </div>
94: <div style="width:900px;float:left;margin-left:29px;margin-top:15px;"><p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;">Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge</p>
95: <%= simple_form_for(:konkurrancer, :url => '/finder', :method => "post") do |f| %>
96: <%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
97: <%= f.button :submit, :style => 'display: none;' %>
98: <% end %>
99:
我尝试将其更改为实例变量并收到此错误:
NoMethodError in Public#index
Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised:
undefined method `to_key' for #<Class:0x85246c8>
Extracted source (around line #95):
92: <%= render("shared/forside") %>
93: </div>
94: <div style="width:900px;float:left;margin-left:29px;margin-top:15px;"><p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;">Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge</p>
95: <%= simple_form_for(@konkurrancer, :url => '/finder', :method => "post") do |f| %>
96: <%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
97: <%= f.button :submit, :style => 'display: none;' %>
98: <% end %>
我的 Jquery:
$(document).ready(function() {
// send form ved klik paa listen
$('option').click(function ()
{
var form=$(this).closest('form');
$.ajax({
type:'post',
url:form.attr('action'),
data:form.serialize(),
success:function(msg){
$('#formcontent').html(msg);
}
});
});
});
I am trying to create a form with the gem simple form.
The form should have a select list of all konkurrancers. And the action URL should be the controller public and action pricecompare and with the ID of the konkurrancer. My route: pricecombare/:id
My route.rb:
match "/finder/:id" => 'public#pricecompare'
My simple form so far:
<%= simple_form_for({:controller => "public", :action => "pricecompare"}, :method => "get") do |f| %>
<%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
<%= f.button :submit, :style => 'display: none;' %>
I get this error:
undefined method `model_name' for Hash:Class
The Html output I want:
<form method="post" id="new_konkurrancer" enctype="multipart/form-data" class="simple_form konkurrancer" action="/public/pricecompare" accept-charset="UTF-8">
<select style=" margin-left:10px;width:370px;float: left;
margin-top: 10px;"name="konkurrancer[form]" id="konkurrancer_form" class="select optional"><option value="">Vælg din A-kasse:</option>
<option value="1">ASE</option>
<option value="2">Træ-industri-byg</option>
<option value="3">Journalistik, kommunikation og sprog</option>
<option value="4">Faglis fælles A-kasse (3F)</option>
<option value="5">Danske lønmodtagere</option>
</select>
<input type="submit" value="Opret konkurrence" name="commit" id="konkurrancer_submit" class="button" style="display: none;">
Updated:
My public controller:
def index
@konkurrancer = Konkurrancer
end
def pricecompare
@akasse = Konkurrancer.where(params[:id]).first
@akasserne = Konkurrancer.order(sort_column + " " + sort_direction)
end
<%= simple_form_for(:konkurrancer, :url => {:controller => "finder"}, :method => "get") do |f| %>
<%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
<%= f.button :submit, :style => 'display: none;' %>
<% end %>
And I get this error in view:
ActionController::RoutingError in Public#index
Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised:
No route matches {:controller=>"finder"}
Extracted source (around line #95):
92: <%= render("shared/forside") %>
93: </div>
94: <div style="width:900px;float:left;margin-left:29px;margin-top:15px;"><p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;">Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge</p>
95: <%= simple_form_for(:konkurrancer, :url => {:controller => "finder"}, :method => "get") do |f| %>
ActionController::RoutingError in Public#index
Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised:
No route matches {:controller=>"finder"}
Update NEW error log:
Association cannot be used in forms not associated with an object
Extracted source (around line #96):
93: </div>
94: <div style="width:900px;float:left;margin-left:29px;margin-top:15px;"><p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;">Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge</p>
95: <%= simple_form_for(:konkurrancer, :url => '/finder', :method => "post") do |f| %>
96: <%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
97: <%= f.button :submit, :style => 'display: none;' %>
98: <% end %>
99:
I have tried to change it to a instanse variable and get this error:
NoMethodError in Public#index
Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised:
undefined method `to_key' for #<Class:0x85246c8>
Extracted source (around line #95):
92: <%= render("shared/forside") %>
93: </div>
94: <div style="width:900px;float:left;margin-left:29px;margin-top:15px;"><p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;">Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge</p>
95: <%= simple_form_for(@konkurrancer, :url => '/finder', :method => "post") do |f| %>
96: <%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
97: <%= f.button :submit, :style => 'display: none;' %>
98: <% end %>
My Jquery:
$(document).ready(function() {
// send form ved klik paa listen
$('option').click(function ()
{
var form=$(this).closest('form');
$.ajax({
type:'post',
url:form.attr('action'),
data:form.serialize(),
success:function(msg){
$('#formcontent').html(msg);
}
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,既然你想要一个带有菜单的跳转导航,你可以像这样实现它,只是为了给你一个想法。它默认使用 javascript 和 jquery,但如果 javascript 不可用,则有后备方案。
在您看来:
在您的 application.js 中或者如果您使用的是 3.1 资产,请放置在适当的文件中。
确保 jquery 和应用程序包含在您的 javascript_include_tags 中。
最后在你的控制器中。使用准备语句将有助于防止 SQL 注入。
我不知道 Konkurrancer 是什么意思,所以相应地调整标签。
Ok, since you want a jump navigation with a menu you can implement it something like this, just to give you an idea. It uses javascript and jquery by default but has a fallback if javascript isn't available.
In your view:
In your application.js or if you are using 3.1 assets, place in appropriate file.
Make sure jquery and application are included in your javascript_include_tags.
Then finally in your controller. Using a prepare statement will help prevent a SQL injection.
I have no idea what Konkurrancer means so adjust labels accordingly.