有一个响应式按钮

Have a responsive button

本文关键字:按钮 响应 有一个      更新时间:2023-09-26

我是RoR的新手,我需要一些建议

为了解释一般概念,用户可以创建私有的配方(post)。一旦,用户可以发布帖子,添加价格和数量。一堵墙引用了所有最后发表的帖子。

现在,我打算制作一个按钮,将我的私人帖子发布到公共墙上。我不确定如何做到这一点。按钮

理想情况下,我将能够为每个新出版物编辑价格和数字

我目前正在建立帖子,成分,评论,用户和用户个人资料。

应该为此操作生成脚手架或向我的帖子添加新闻变量?

如果你想看我的代码=

post_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>

和帖子2 :

<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
您可以

为帖子添加标志,例如is_public,并在创建新帖子时默认将其设为 false,这意味着帖子是私有的。

当用户想要公开帖子时,他们需要单击按钮:

<%= link_to "-  Pusher  - ", post_path(post, is_public: true), method: :put, class: "btn btn-success" %>

这样,您创建了一个链接作为引导按钮,该按钮引用了以下内容:

/

posts/:id, method: :p ut.

在您的posts_controller中,您可以通过params[:is_public]捕获此信息并成功更新您的帖子。

这样,您就可以保持 RESTfull,而无需在控制器中执行任何附加功能。

其余地,您应该对/wall/posts执行POST操作:

  • 拍摄Post的 ID、价格和照片
  • 按 ID 查找Post
  • 更新价格/照片参数
  • 将"公共"标志设置为 true

您还应该在以下/wall/posts/:id有一个DELETE操作:

  • 按 ID 查找Post
  • 将"公共"标志设置为 false

这会从墙上删除Post,但不会删除Post本身。

您可以将URL更改为/wall或更漂亮的内容,但我故意使其更加面向资源,以便您可以看到正在发生的事情。

现在,按钮本身(我认为基于您想要更多信息的事实意味着您需要一个表单来配合它),将需要 POST 到 /wall/posts .

所以,综上所述,我认为这是"正确"的方式。 但是要回答我认为您要提出的问题,您还可以通过更新routes.rb向当前控制器添加一个单独的方法:

resources :posts do
  post :publish, on: :member
end

这将产生像 /posts/1/publishpublish_post_path(@post) 这样的路由作为帮助程序方法。 然后,您可以在posts_controller中创建一个名为 publish 的函数来处理该路由。 现在,这在功能意义上似乎更合乎逻辑,但它脱离了 REST。但是,这是可以做到的。 我认为以上更合乎逻辑,但这只是我的想法。

作为练习,您还可以以相同的方式创建"取消发布"路由,也可以使用它来删除墙柱。