I am working on a form for a editorial calendar app. I have two things going out that are pretty similar and not working.
Working with 3 models: Platforms, Posts and Calendars. They are join tables. Platform <=> Post, Post <=> Calendars
Post/new & Post/edit form:
<div class="container">
<div class="form-field">
<%= form_for @post do |f| %>
<%= f.label :title %>
<%= f.text_field :title, required: true %> <br>
Title is required.
</div>
<div class="form-field">
<%= f.label :content%>
<%= f.text_area :content%>
</div>
<div class="form-field">
<%= f.label :link %>
<%= f.text_field :link %>
</div>
<div class="file-field">
<%= f.label :picture%>
<%= f.file_field :picture, id: :post_picture%>
</div>
<%= f.hidden_field :user_id %> <br>
<div class="form-field">
<%= f.fields_for :platform_attributes do |platform| %>
<%= platform.label :platform, "Social Platforms"%>
<%= platform.collection_check_boxes :platform_ids, Platform.all, :id, :name %> <br> <br>
</div>
<div>
<h4> Or Create a new platform: </h4>
<%= platform.label :platform, 'New Platform'%>
<%= f.fields_for :platform_attributes, @post.platforms.build do |platform| %>
<%= platform.text_field :name%> <br> <br>
<% end %>
</div>
<% end %>
<%= f.submit class: "btn btn-sm btn-info"%>
<% end %>
</div>
My post controller is handling the checkboxes issue, and the "schedule post" issue. It will only allow me to schedule for one calendar, and it does not save the updates and add additional calendars.
Posts Controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :schedule_post, :destroy]
def new
@posts = current_user.posts.select {|p| p.persisted?}
@post = current_user.posts.build
@platforms = Platform.all
end
def edit
@calendars = current_user.calendars
@platforms = Platform.all
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to post_path(@post)
else
redirect_to new_post_path
end
end
def update
@post.update(post_params)
if @post.save
redirect_to post_path(@post), notice: 'Your post has been updated.'
else
redirect_to edit_post_path(@post)
end
end
def schedule_post
@calendar_post = CalendarPost.new(calendar_post_params)
if @calendar_post.save
binding.pry
redirect_to post_path(@post)
else
render 'show'
end
end
private
def set_post
@post = Post.find(params[:id])
end
def set_calendars
@calendars = current_user.calendars
end
def post_params
params.require(:post).permit(:title, :content, :link, :finalized, :picture, :user_id, :platform_attributes => [:name])
end
def calendar_post_params
params.require(:calendar_posts).permit(:post_id, :calendar_id, :date, :time)
end
end
I want the user to be able to add a post to multiple platforms and multiple calendars because of the versatility of what someone may need.
I also have my setter in my Post model.
class Post < ApplicationRecord
has_many :calendar_posts
has_many :calendars, through: :calendar_posts
has_many :platform_posts
has_many :platforms, through: :platform_posts
belongs_to :user
def platform_attributes=(platform_attributes)
platform_attributes.values.each do |platform_attribute|
platform = Platform.find_or_create_by(platform_attribute)
self.platforms << platform
end
end
thoughts? why are they not saving to more than one calendar or more than one platform if they choose to have more than one?
Aucun commentaire:
Enregistrer un commentaire