我是 RoR 的新手,我需要一些建议
为了解释一般概念,用户可以创建私有(private)的食谱(帖子)。然后,用户可以发布添加价格和数量的帖子。一面墙引用了所有最后发布的帖子。
现在,我打算制作一个按钮,将我的私有(private)帖子发布到公共(public)墙上。而且我不确定这样做的方法。 Pushing button
理想情况下,我可以为每个新发布编辑价格和编号
我目前正在构建帖子、成分、评论、用户和用户个人资料。
应该为此操作生成脚手架还是向我的帖子添加新闻变量?
如果你想看我的代码=
后 Controller :
class PostsController < ApplicationController
before_action :authenticate_user!
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :owned_post, only: [:edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all.order("created_at DESC")
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = current_user.posts.build
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = current_user.posts.build(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:user_id, :title, :description, :image, ingredients_attributes: [:id, :name, :_destroy])
end
def owned_post
unless current_user == @post.user
flash[:alert] = "That post doesn't belong to you!"
redirect_to root_path
end
end
end
个人资料/节目:
<div class="row profile-header">
<div class="col-md-6">
<div class="img-circle">
<%= profile_avatar_select(@user) %>
</div>
</div>
<div class="col-md-6">
<div class="user-name-and-follow">
<h3 class="profile-user-name">
<%= @user.pseudo %>
</h3>
<span>
<% if @user == current_user %>
<%= link_to 'Edit Profile', edit_profile_path(@user.pseudo) %>
<% end %>
</span>
</div>
<p class="profile-bio">
<%= @user.bio %>
</p>
<div class="user-statistics">
</div>
</div>
</div>
</div>
<p>
<%= pluralize @user.posts.count, 'post' %>
</p>
<div>
<%-@posts.each do |post| %>
<div class="toca">
<%= render 'posts/post2', post: post %>
<% end %>
</div>
</div>
和 post2 :
<div class="row">
<%-@posts.each do |post|%>
<div class="post">
<div class="form-group text-center">
<h3> <%=post.title%></h3>
</div>
<p> Posted by : <%= link_to post.user.pseudo, profile_path(post.user.pseudo) %>, <%= time_ago_in_words(post.created_at) %> ago </p>
<div class="image text-center">
<div class="image-border">
<%= link_to (image_tag post.image.url(:medium), class: 'img-responsive'), post_path(post)%>
</div>
</div>
<div class="btn-group" role="group" aria-label="...">
<%= link_to '- Pusher - ', post, class: 'btn btn-primary' %>
</div>
</div>
<% end %>
</div>
</div>
所以如果你有任何建议可以向我建议最适合你的方法
最佳答案
<%= link_to '- Pusher - ', post, class: 'btn btn-primary' %>
您可以创建一个新方法来更新属性:
<%= link_to '- Pusher - ', post, class: 'btn btn-primary', controller: "post", action: :go_public %>
然后就可以写代码公开了:
def go_public(params)
@post = Post.find(id)
@post.update_attributes(public_boolean: true)
end
关于javascript - 有一个响应按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36777444/