Hello, good day!
This is a rather intricate question coming from a not so experienced ruby on rails user. Let’s get to it:
I am developing an app that has users, authors, microposts and tags. I want to implement something like a retweet button for the microposts:
The task is simple:
Get the params of a micropost(original user, original author, original tags)
“Retweet” it to my own wall, using these params, but also with my own user_id embedded in it.
So far I managed to “retweet” it, but there is one problem. As it is now, I am copying all the params and creating a new micropost with these params.
As this creates exact duplicate, I would like to know what would be the best approach to implement this function without creating replicas all over my app.
Here’s the codes:
ROUTES.rb
resources :microposts do
member do
get :retweet
end
end
MICROPOSTS_CONTROLLER.rb
def retweet
original_micropost = Micropost.find(params[:id])
if original_micropost
new_micropost = current_user.microposts.build(content: original_micropost.content, author_id: original_micropost.author_id)
if new_micropost.save
redirect_to user_path(current_user)
flash[:success] = “Retweet Successful”
else
redirect_to user_path(current_user), notice: new_micropost.errors.full_messages
end
else
redirect_back_or current_user
flash[:error] = “Retweet error!”
end
end
_MICROPOST.html.erb
<%= link_to (image_tag “retweet.png”), retweet_micropost_path(micropost) %>
Let me know if there is anything else needed to get around this.
Thank you in advance.