gemのシンプルなフォームでフォームを作成しようとしています。
フォームには、すべてのkonkurrancersの選択リストが含まれている必要があります。また、アクションURLは、コントローラーpublicとaction pricecompareであり、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);
}
});
});
});