简体   繁体   中英

Rails 4 - Simple form arguments

Can anyone see what's wrong with this form of expression?

<%= simple_fields_for :article do |f| %>
    <%=f.input :q, :nil, placeholder: "Search" %>
<% end %>

Error message says:

wrong number of arguments (3 for 1..2)

So- my wider code is:

articles#index.html.erb:

     <div class="row">
        <div class="col-md-10 col-md-offset-1">
        <%= render 'articles/searchform' %>
        </div>

        <div class="col-md-10 col-md-offset-1">
          <% Article.all.sort_by(&:created_at).in_groups_of(3) do |group| %>

</div>


     <div class="row">
          <% group.compact.each do |article| %>
              <div class="col-md-4">
                <div class="indexdisplay">
                  <%= image_tag article.image_url, width: '100%', height: 

        '200px' if article.image.present? %>
        <div class="indexheading"> <%= link_to article.title, article %> </div>
        <div class="indexsubtext">    <%= truncate(article.body, :ommission =>  

    "...", :length => 250) %></div>

                        </div>
                      </div>
                        <% end %>


                </div>



     <div class="row">
          <%= link_to 'New Article', new_article_path %>
     </div>  

In my views articles#form.html.erb

        <div class="col-xs-10 col-xs-offset-1">
            <%= simple_form_for(@article) do |f| %>
                <%= f.error_notification %>

                    <div class="form-inputs">
                        <%= f.input :title,  autofocus: true %>
                        <%= f.input :body, :label => "Post", :input_html => {:rows => 10} %>
                        <%= f.input :image, :label => "Add an image" %>

                    </div>

                    <div class="form-actions">
                        <%= f.button :submit, "Review a draft", :class => 

        'formsubmit' %>
                        </div>
                <% end %>

In articles#searchform.html.erb

     <% f.simple_fields_for :article do |a| %>

          <%=a.input :q, :nil, placeholder: "Search" %>

     <% end %>

I get this error:

undefined local variable or method `f' for #<#<Class:0x007f874133c410>:0x007f8740448058>

You are missing your form variable, eg f .

For instance if you have simple_form_for ... do |f| you should then write

= f.simple_fields_for :article do |article|
  = article.input :q, :nil, placeholder: 'search'    

[EDIT] You seem confused. The simple_fields_for is a special command to iterate over nested items, inside a form (eg comments for a post). So simple_fields_for would require a surrounding form ( simple_form_for ). For your search-form you should just be using simple_form_for .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM