TAG FUNCTION A METHOD FOR TAG INFORMATION TRANSMITTED BY STRONG PARAMETER BY TAG FUNCTION AND STORAGE

Asked 2 years ago, Updated 2 years ago, 37 views

Currently, we are trying to implement hashtag functionality in the portfolio posting function.

We plan to encourage you to fill out the post form as follows:

"Tags (Please include half-width , between tags)" → (Example) A, B, C

At that time, the value sent in the strong parameter is params{tagname:"A,B,C"}.
When I save this with the create method, I would like to use .split(",") to save tag information one by one by one.However, I don't know how to describe it.I would like your advice on how to describe it in the controller.

The current controller is attached.
This time, we are using Form objects to implement the tag function.

class PostsController<ApplicationController
  def new
    @post_form=PostForm.new
  end

  def create
    @post_form=PostForm.new(post_form_params)
    if@post_form.va lid?
      @post_form.save
      redirect_to root_path
    else
      render:new
    end
  end

  def beautypage
    @beautypages=Post.where(genre:1)
  end

  default page
    @troublepages=Post.where(genre:2)
  end


  private 

  def post_form_params
    param.require(:post_form).permit(:title,:genre,:detail,:tagname,images:[]).merge(user_id:current_user.id)
  end
end

Below is the content of the post form

<%=form_with model:@post_form, url:url, method:method, id:'new_post', local:true do|f|%>
  <div class="field">
    <%=f.label: title, "Title" %><br/>
    <%=f.text_field:title, id: "post_title"%>
  </div>

  <div class="field">
    <%=f.label:images, "image"%><br/>
    <%=f.file_field:images, multiple:true, id:"post_image"%>
  </div>

  <div class="field">
    <%=f.label:genre, "Posting Genre" %><br/>
    <%=f.select:genre,[["Beauty Post",1],["Concern Post",2]], include_blank:"Please Select", id:"post_genre"%>
  </div> 

  <div class="field">
    <%=f.label:detail, "Post Content" %><br/>
    <%=f.text_area:detail,class::form_text,id:"post_detail"%>
  </div>

  <div class="field">
    <%=f.label:tagname, "tag (please include half-width "," between tags), {class:"tag-label"}%>br/>
    <%=f.text_field:tagname,placeholder:"(e.g.) Skin care, dry skin, rough skin",:class=>"tag-field"%>
  </div>


  <div class="actions">
    <%=f.submit "Post", class::form__btn%>
  </div>
<%end%>

Contents of the Form object below → Would it be better to remove the , and save it here?

class PostForm
  include ActiveModel::Model
  attr_accessor:title, :images, :detail, :genre, :tagname, :user_id, :post_id, :tag_id

  with_options presence —true do
    values:title, length: {maximum:50}
    validates —genre
    validates:detail
    validates:user_id
  end

  def save
    post=Post.create(title:title, genre:genre, detail:detail, user_id:user_id, images:images)
  
    tag = Tag.where(tagname:tagname).first_or_initialize
    tag.save
    
    PostTag.create(post_id:post.id, tag_id:tag.id)
  end
end

ruby-on-rails ruby

2022-09-30 16:31

1 Answers

If you are using Form Object, it is desirable to do so when processing parameters like this.Basically, the controller should be thin.

In terms of code,

tagname.split(', ').each do|name|
  tag = Tag.find_or_create_by (tagname:name)
  PostTag.create(post_id:post.id, tag_id:tag.id)
end

It's kind of like that.


2022-09-30 16:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.