Note that there are some explanatory texts on larger screens.

plurals
  1. PORails help simple form: undefined method `model_name' for Hash:Class
    text
    copied!<p>I am trying to create a form with the gem simple form.</p> <p>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 </p> <p>My route.rb:</p> <pre><code>match "/finder/:id" =&gt; 'public#pricecompare' </code></pre> <p>My simple form so far: </p> <pre><code>&lt;%= simple_form_for({:controller =&gt; "public", :action =&gt; "pricecompare"}, :method =&gt; "get") do |f| %&gt; &lt;%= f.association :konkurrancer, :collection =&gt; Konkurrancer.all(:order =&gt; 'name'), :prompt =&gt; "Pick company" %&gt; &lt;%= f.button :submit, :style =&gt; 'display: none;' %&gt; </code></pre> <p>I get this error:</p> <pre><code>undefined method `model_name' for Hash:Class </code></pre> <p>The Html output I want:</p> <pre><code>&lt;form method="post" id="new_konkurrancer" enctype="multipart/form-data" class="simple_form konkurrancer" action="/public/pricecompare" accept-charset="UTF-8"&gt; &lt;select style=" margin-left:10px;width:370px;float: left; margin-top: 10px;"name="konkurrancer[form]" id="konkurrancer_form" class="select optional"&gt;&lt;option value=""&gt;Vælg din A-kasse:&lt;/option&gt; &lt;option value="1"&gt;ASE&lt;/option&gt; &lt;option value="2"&gt;Træ-industri-byg&lt;/option&gt; &lt;option value="3"&gt;Journalistik, kommunikation og sprog&lt;/option&gt; &lt;option value="4"&gt;Faglis fælles A-kasse (3F)&lt;/option&gt; &lt;option value="5"&gt;Danske lønmodtagere&lt;/option&gt; &lt;/select&gt; &lt;input type="submit" value="Opret konkurrence" name="commit" id="konkurrancer_submit" class="button" style="display: none;"&gt; </code></pre> <p><strong>Updated:</strong></p> <p>My public controller:</p> <pre><code>def index @konkurrancer = Konkurrancer end def pricecompare @akasse = Konkurrancer.where(params[:id]).first @akasserne = Konkurrancer.order(sort_column + " " + sort_direction) end &lt;%= simple_form_for(:konkurrancer, :url =&gt; {:controller =&gt; "finder"}, :method =&gt; "get") do |f| %&gt; &lt;%= f.association :konkurrancer, :collection =&gt; Konkurrancer.all(:order =&gt; 'name'), :prompt =&gt; "Pick company" %&gt; &lt;%= f.button :submit, :style =&gt; 'display: none;' %&gt; &lt;% end %&gt; </code></pre> <p>And I get this error in view:</p> <pre><code>ActionController::RoutingError in Public#index Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised: No route matches {:controller=&gt;"finder"} Extracted source (around line #95): 92: &lt;%= render("shared/forside") %&gt; 93: &lt;/div&gt; 94: &lt;div style="width:900px;float:left;margin-left:29px;margin-top:15px;"&gt;&lt;p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;"&gt;Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge&lt;/p&gt; 95: &lt;%= simple_form_for(:konkurrancer, :url =&gt; {:controller =&gt; "finder"}, :method =&gt; "get") do |f| %&gt; ActionController::RoutingError in Public#index Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised: No route matches {:controller=&gt;"finder"} </code></pre> <p>Update NEW error log:</p> <pre><code>Association cannot be used in forms not associated with an object Extracted source (around line #96): 93: &lt;/div&gt; 94: &lt;div style="width:900px;float:left;margin-left:29px;margin-top:15px;"&gt;&lt;p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;"&gt;Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge&lt;/p&gt; 95: &lt;%= simple_form_for(:konkurrancer, :url =&gt; '/finder', :method =&gt; "post") do |f| %&gt; 96: &lt;%= f.association :konkurrancer, :collection =&gt; Konkurrancer.all(:order =&gt; 'name'), :prompt =&gt; "Pick company" %&gt; 97: &lt;%= f.button :submit, :style =&gt; 'display: none;' %&gt; 98: &lt;% end %&gt; 99: </code></pre> <p>I have tried to change it to a instanse variable and get this error:</p> <pre><code>NoMethodError in Public#index Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised: undefined method `to_key' for #&lt;Class:0x85246c8&gt; Extracted source (around line #95): 92: &lt;%= render("shared/forside") %&gt; 93: &lt;/div&gt; 94: &lt;div style="width:900px;float:left;margin-left:29px;margin-top:15px;"&gt;&lt;p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;"&gt;Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge&lt;/p&gt; 95: &lt;%= simple_form_for(@konkurrancer, :url =&gt; '/finder', :method =&gt; "post") do |f| %&gt; 96: &lt;%= f.association :konkurrancer, :collection =&gt; Konkurrancer.all(:order =&gt; 'name'), :prompt =&gt; "Pick company" %&gt; 97: &lt;%= f.button :submit, :style =&gt; 'display: none;' %&gt; 98: &lt;% end %&gt; </code></pre> <p>My Jquery:</p> <pre><code>$(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); } }); }); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload