1

Rails 4 の im noob 、投稿があります。投稿には has_many カテゴリがあり、ビューにはカテゴリへのリンクがあります。次のようになります。

<ul class="dropdown-menu">
      <% Category.all.each do |category| %>
      <li><%= link_to category.name, new_post_path %> </li>
      <% end %>
    </ul>

ユーザーをフォームにレンダリングすると、次のようになります。

    <%= form_for current_user.posts.build(params[:post]) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <h2>Заполните заявку</h2>
    <div class="text-container">
    <p>
  Мне нужно
    </p>
     <div class="field">
    <p>
    Подробно опишите задание
    </p>
    <%= f.collection_select :category_id, Category.all, :id, :name %>
    <%= f.text_area :content, placeholder: "Compose new micropost..." %>
    <%= f.date_select :date %>
    <%= f.time_select :time %>
  </div>
</div>
  <%= f.submit "Опубликовать", class: "btn btn-large btn-primary" %>
<% end %>

ユーザーがカテゴリを選択すると、

<ul class="dropdown-menu">
          <% Category.all.each do |category| %>
          <li><%= link_to category.name, new_post_path %> </li>
          <% end %>
        </ul>

フォームページのカテゴリは最初のカテゴリですが、カテゴリを選択する必要があります

<ul class="dropdown-menu">
              <% Category.all.each do |category| %>
              <li><%= link_to category.name, new_post_path %> </li>
              <% end %>
            </ul>


class PostsController < ApplicationController
 # before_filter :signed_in_user

  def new
  end

  def index
    @posts = Post.all
  end

  def show
    redirect_to root_url
  end

  def create
    @post = current_user.posts.build(post_params)

    if @post.save
      flash[:success] = "Поздравляем Ваше задание опубликованно"
      redirect_to @post
  else
    render 'posts/new'
  end
end

private
  def post_params
    params.require(:post).permit(:content, :date, :time, :category_id)
  end

  def correct_user
      @post = current_user.posts.find_by_id(params[:id])
      redirect_to root_url if @post.nil?
    end
end

Rails 4.0.0 と Ruby 2.0 で作業しています。これを機能させるにはどうすればよいですか? 何か案は?

4

1 に答える 1