Etsy Demo: Allowing users to create only one listing at a time

Currently users have the option of listing as many listings as they want. I'd like them to be able to create only one listing and if they do then the "new profile" goes away. I set my association already to: listing belongs_to user user has_one Thank you!

Posted over 4 years ago by Omar Amiri
Posted about 4 years ago by Jenson Vaj

Etsy demo allows only users to create only one listing at a time. Help. Here is my video Alex - http://youtu.be/wyWy31PiOfA.

1
Posted about 4 years ago by Jenson Vaj

Here is the video which is public - https://youtu.be/wyWy31PiOfA

1
Posted about 4 years ago by Alex Yang

Are you sure your code matches that of the Etsydemo app exactly? It looks like you've made customizations, and those might be the reason why you're seeing this issue. I'll need more information to help you debug...go to your Stripe account and click on 'Logs'. There you should be able to see the details of your failed request.

1
Posted about 4 years ago by Alex Yang

Seems like the video is private. Please make it public or use CloudApp to record a video instead.

1
Posted about 4 years ago by Jenson Vaj

Here is the video which is public - https://youtu.be/wyWy31PiOfA

1
Posted over 4 years ago by Jenson Vaj

Paginate issue in my project. Not sure what is going on. Code is exactly as mentioned in the video. Any help is greatly appreciated. The screen shot is here - http://cl.ly/image/310l2X0u0n3z

1
Posted over 4 years ago by Alex Yang

Please re-post this in the 'Discussion' section beneath the video on pagination.

1
Posted over 4 years ago by Jenson Vaj

See better view. Not sure how to provide you a snap shot on your forum.

handle_unverified_requestactionpack (4.0.0) lib/action_controller/metal/request_forgery_protection.rb
158
159
160
161
162
163
164
165
166
167
168
def initialize(controller)
@controller = controller
end
def handle_unverified_request
raise ActionController::InvalidAuthenticityToken
end
end
end
protected

1
Posted over 4 years ago by Jenson Vaj

need help. what does this mean? ActionController::InvalidAuthenticityToken

1
Posted over 4 years ago by Alex Yang

It would help if you could provide some context. I don't know what you're trying to build, what changes you made before the error appeared, and haven't seen a screenshot of the error itself.

1
Posted over 4 years ago by Jenson Vaj

when i try to create a new listings, this pops up. I have not made much changes to your original app.


def initialize(controller)
@controller = controller
end
def handle_unverified_request
raise ActionController::InvalidAuthenticityToken
end
end
end
protected

1
Posted over 4 years ago by Alex Yang

Sorry, I'll need more info from you to be able to help:
1) What are you actually trying to do? Build a new feature? Customize the Etsydemo app?
2) What changes did you make immediately before seeing the error?
3) When did you encounter the error (e.g. visiting a page, submitting a form, etc)?

If you need to send a screenshot in the future, please paste a link here (I recommend using CloudApp to do so).

1
Posted over 4 years ago by Jenson Vaj

Alex, my responses are below:
#1) I was trying to upload additional products and was not able to.
#2) I made no changes
#3) errors shows up when I try to upload a second product. This happens to new and existing users in my database. I invited you in CloudApp to see my screenshot. Thanks for letting me know about it.

1
Posted over 4 years ago by Alex Yang

Jenson, your email invited me to use CloudApp, but I can't actually see the screenshot. After taking the screenshot in CloudApp, you'll be able to just paste a link so others can view it.

1
Posted over 4 years ago by Jenson Vaj

Alex, thanks for the patience. The link is here - http://cl.ly/image/1f2j0c0K382F

1
Posted over 4 years ago by Alex Yang

Isn't this a totally different error? This is a Stripe error - I'd recommend reviewing the Etsydemo section on Stripe. Either your code isn't right, or it's a database issue where your specific user has invalid data.

1
Posted over 4 years ago by Alex Yang

Yes, belongs_to/has_one sounds like the right association to use in this case. I'm not sure what you mean by the 'new profile' though - can you clarify this? Is it a new user profile, a new listing profile, or something else?

Note that you'll probably want to also customize the view pages some more. For example, you'll need to remove the 'Add Listing' button for users who have already added a listing, change the seller dashboard from a table layout to one that's more appropriate given that sellers can only have one listing, etc.

1
Posted over 4 years ago by Omar Amiri

As in a new user profile. A user that signs up will have an account settings page, consumer-facing profile page, and order history page. People can hire Musicians for their time (like Code Mentor) so after a user signs up they need to create their profile with all their information. So instead of creating a listing scaffold, I created a Profile scaffold.

How do I remove the "Add Listing" button after they created a listing? I've looked on Stack Overflow on hiding lines of code based on an "if" statement but drew blank.

I tried this with an error:
<% if (user_signed_in?) && (current_user.id != profile.user_id) %>
<%= link_to 'New Profile', new_profile_path, class:"btn btn-link" %>
<% end %>

1
Posted over 4 years ago by Alex Yang

I think I understand. You want to know how to conditionally display the 'New Listing' button (renamed the 'New Profile' button in your app), but only for users who haven't already added any Listings/Profiles.

The reason why your code doesn't work is probably because 'profile' isn't defined properly, so it equals nil. If 'profile' is nil, you can't use '.user_id' because it'll throw an error. Instead, you should try something like this:

<% if (user_signed_in?) && (Profile.where(user_id: current_user.id).present?) %>

This checks the Profiles database to see if any profiles have a user_id that matches the current user's ID. If so, '.present?' will evaluate to true.

An alternative, more efficient approach would be to add a boolean (true/false) field (let's call it 'has_profile') to your User database that indicates whether the user has created a profile. 'has_profile' would start as false by default, become true when a profile is created, and revert back to false when the profile is destroyed. With this approach, you'd only need to check whether has_profile is true or false to determine whether to display the 'New Profile' button.

1
Posted over 4 years ago by Omar Amiri

Thanks Alex. I tried using the if statement but it hides the "new profile" altogether. Instead I tried
<% if (user_signed_in?) && (current_user.profile.blank?) %> and it works.

I'm at the part of adding recipients to the marketplace and found out Stripe deprecated recipients and instead using Accounts. https://stripe.com/docs/connect/migrating

Does that change how we integrate transferring funds now?

1
Posted over 4 years ago by Alex Yang

Great - that 'if' statement should do the trick! As for Stripe, if you're already setup with recipients, you should be fine using that until the end of the year, but I'd encourage you to take a look at accounts now (or at least in July once accounts is no longer in beta). There should only be two sections of code you need to change:

1) Where you create a recipient (listings_controller.rb)

Instead of using the recipient code (e.g. Stripe::Recipient.create()...), you should use the account code, which looks like this:

Stripe::Account.create(
:managed => false,
:country => 'US',
:email => 'bob@example.com'
)

You can find more details here: https://stripe.com/docs/api?lang=ruby#create_account

2) Where you transfer funds (orders_controller.rb)

You'll need to change the 'recipient' parameter in your transfer code and replace it with the 'destination' parameter. You can read more about that here: https://stripe.com/docs/api?lang=ruby#create_transfer.

Let me know how it goes.

1
Posted over 4 years ago by Omar Amiri

Works great!!! Thank you so much.

1