我是 Rails 4 中的菜鸟,我有帖子,帖子 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 上工作我想做什么来完成这项工作?有任何想法吗?