Rails帮助简单形式:Hash:Class的未定义方法`model_name'

时间:2011-06-26 18:13:47

标签: ruby-on-rails ruby ruby-on-rails-3 simple-form

我正在尝试使用gem simple form创建一个表单。

表单应该包含所有konkurrancers的选择列表。操作URL应该是控制器public和action pricecompare以及konkurrancer的ID。我的路线:pricecombare /:id

我的路线.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: 

我试图将其更改为instanse变量并收到此错误:

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);
          }
        });
    });
});

1 个答案:

答案 0 :(得分:0)

好的,既然你想要一个带菜单的跳转导航,你就可以实现这样的东西,只是为了给你一个想法。它默认使用javascript和jquery,但如果javascript不可用,则会有后备。

在您看来:

<%= form_tag "/finder/", :method => "get" %>
<%= label_tag :jump_nav, "Company" %>
<%= select_tag(:id, options_for_select(Konkurrancer.all(:order => 'name').collect { |k| [k.name, k.id] }), :id => "jump_nav") %>
<%= submit_tag "Jump to company", :id => "jump_nav_submit" %>

在您的application.js中或如果您使用3.1资产,请将其放在适当的文件中。

$().ready(function() {
    $('#jump_nav_submit').hide();
});

$('#jump_nav').live('change', function() {
    window.location = "\/finder\/" + $(this).val();
});

确保您的javascript_include_tags中包含jquery和应用程序。

然后终于在你的控制器中。使用prepare语句有助于防止SQL注入。

def pricecompare
  @akasse = Konkurrancer.first(:conditions => ["id = ?", params[:id]])
end

我不知道Konkurrancer是什么意思所以相应地调整标签。