weixin_33691700 2016-10-28 17:45 采纳率: 0%
浏览 18

循环通过Form_for rails

I have "rails generated scaffold Post" and im trying to convert the posts.each index to form_for so I can use "remote: true" the AJAX.

Problem: My code doesn't show my records. It just displays the table thread headings.

    class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
    @post = Post.new
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

^^^ So i have only made these change to the controller and the following code is failing to display the post records

<tbody>
<% @posts.each do |mend| %>
<% form_for(mend) do |f| %>
  <tr>
    <td><%= f.text_field :title %></td>
    <td><%= f.text_field :content %></td>
    <td><%= f.text_field :verify %></td>
    <td><%= f.text_field :date %></td>
    <td><%= f.text_field :rate %></td>
    <td><%= link_to 'Show', post_path(f) %></td>
    <td><%= link_to 'Edit', edit_post_path(f) %></td>
    <td><%= link_to 'Destroy', @post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
<% end %>
  • 写回答

2条回答 默认 最新

  • derek5. 2016-10-28 19:56
    关注

    You should render your form_for line, it's not enough just to render fields.

    Try using:

    <%= form_for(mend) do |f| %>
    

    instead of:

    <% form_for(mend) do |f| %>
    
    评论

报告相同问题?