(强制性的,我是编码确认的新手)
我已经使用 Rails 创建了一个简单的博客应用程序,并且还为我的表单使用了 simple_for gem。我正在尝试使用两个提交按钮创建一个草稿状态。最终目标是让“发布”按钮让所有登录或未登录的用户在索引页面上看到帖子,而“另存为草稿”按钮让只有登录的人才能看到帖子。这是我到目前为止所拥有的。
_form.html.erb:
<div class="actions">
<p><%= f.submit "Publish", :name => "publish" %>
<%= f.submit "Save as Draft", :name => "draft" %></p> </div>
posts_controller.rb:(不太确定在这里放什么)
def create
@post = current_user.posts.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
index.html.erb:(不确定如何指定使用不同值提交的帖子)
<% @posts.reverse_each do |post| %>
<h1 style="font-family: latohairline"><%= link_to post.title, post %></h1>
<p style="margin-top: -20px"><small>Posted by <%= post.user.name %></small></p>
<p><%= post.description.html_safe %></p>
<% if user_signed_in? %>
<div><p><small><%= link_to 'Edit', edit_post_path(post) %> |
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' } %></small></p></div>
<% else %>
<% end %>
post.rb:(不确定模型中是否需要更改任何内容,但以防万一)
class Post < ActiveRecord::Base
validates_uniqueness_of :title
def to_param
[id, title.parameterize].join("-")
end
attr_accessible :description, :title, :image
validates :user_id, presence: true
validates :description, presence: true
belongs_to :user
has_attached_file :image
end
任何帮助将不胜感激,我已经在谷歌上搜索了几个小时,但是在不知道所有正确的搜索词的情况下,我最终得到了 zilch。谢谢!