Unique Session ids in Rails - ruby-on-rails

I made a shopping cart model for a webapp that I am working on. It was just a standard ruby on rails model.
It lets users add products to the shoppingcart, but only shows it to them if they are the user that added the item. However, I'd like to let users who haven't signed in add items to a shopping cart.
Right now I'm using devise so I can check for a current_user, and assign that shopping cart item to that user. However is there a similar unique session id or anything I can use to simulate a user. So I only show the items that were added to the one person?
The reason for this is I'd like my user to go through as much of the ordering process as possible before asking them to sign-in and make an account.
Thanks

A simple method for this is creating a shopping cart and then adding that identifier to the session regardless of their logged in state.
For example:
#shopping_cart = ShoppingCart.create(user: current_user)
session[:shopping_cart_id] = #shopping_cart.id
Later you can retrieve the current shopping cart, if any, as a before_action handler.

I worked on similar problem. What I did was, I used session_id. First, you need a table(say, product_list) to hold the product list for temporary purpose. Populate each product to this table along with session_id. This will help you to identify the association of product_list with the active user. On checkout copy the record from product_list to your shopping_cart table and delete the record from product_list.
Try this, it might help you with the current problem.

Related

How do associations affect the way we create objects in the controller?

I understand that associations are important because this gives us a way of linking two objects together so they have a 'relation' and we can query through both of them. For example,
A landing page belongs to a blog user & a blog user can have many landing pages
We obviously go into the models and apply the correct methods 'has_many' and 'belongs_to'. We also create a migration and add the foreign key to the 'belongs_to' model. This being the 'landing page'.
My problem:
When creating a landing page, it is possible to choose a blog user. This obviously passes the blog user ID into the params. I want to save this ID into the foreign key field in the landing page model.
Is this possible without doing:
def create
#landing_page = #blog_user.landing_pages.build(landing_pages_params)
end
Why do you have to go through a blog user? Another example:
def new
#landing_page = #blog_user.landing_pages.new
end
What is the purpose of doing it this way? Surely passing the ID into the field is enough without going through the blog_user?
Sure, you could do something like
#landing_page = LandingPages.new(blog_user_id: X)
Obviously, you need to know the ID of the BlogUser record. You'll then be able to access the newly associated BlogUser as you'd expect
#landing_page.blog_user #=> BlogUser(id: X)

Rails adding row to DB through admin approval

I am making an application in Rails and I have Devise about to be included for users management. I have an index page where all the database rows are in with CRUD ability which is all good.
The functionality I'm after is that if a user wants to add a new row, they can go through and 'add' it but before it gets actually added, the request goes through to an admin, then the admin can hit 'approve' and then the row gets added.
The issue is me trying to get around the fact that the program would 'hold' an action of adding a row and the admin would approve it, then the program would let that action apply.
Thanks.
Instead of going through the hassle of preventing saving the row to the DB (you'd have to cancel the saving in a before_create callback but keep the data somewhere anyway and pass them to admin for approval somehow), I'd do a much simpler approach.
Add another column to the table, boolean column named e.g. approved and by default show only all approved rows in the listing. When a user creates a row, save it with approved = false and let admins access these unapproved rows and approve them simply by setting approved = true or delete them if approval is denied.
Also consider creating a DB index for this column for performance reasons.

How can I push an element into a session array when i click a link in my ruby view

I am new to programming in ruby on rails and ruby in general. The website I have created has two models, categories and products, where categories has_many products. The user goes through the categories and picks products and adds them to a cart to buy. I have session[:cart] that will contain the id numbers of each product the user picks. I have the controller and view set up for my cart so that it will show all the products that user picks and totals the price of the cart. There is a link on every "show" view for every product that shows all the values, like name and price, for that product. on that page there is a link that says "ADD TO CART"to the cart page.
My problem is how do i push that product id into the session[:cart] when the i click on the link? do i use a button instead or a post method somehow? i am lost here.
In my opinion you need to use cookies for that because cookies can store more data then session and it store in you browser so you can set them using javascript.
And to add into cart you need to store only ids of products and when every you want to show result just show result on the bases of ids and you can store ids comma separated.

how to add an element to devise session

I am working with rails and devise for authentication, I want to add an element to the session hash, if I was using the native authentication session i would do session[:cart_id], but with devise I dont know if that would work or how to do it the devise way, for now I made a relationship between the customer model and the cart model: a customer has one cart, that way I can access the cart using customer.cart but I saw that many people dont relate the cart to the customer that way, they just create the cart and keep the id in the session hash. so my questions are:
1- How to add an element to the session with devise?
2- Relating the Cart model with the Customer model (a customer has one cart) is a correct approach or should I try to go with the cart_id in the session hash?
Devise don't change anything to the session, it may use keys like :user_return_to (given your devise session is "user") but mostly you are free to use the session as you wish.
Session[:cart_id] is fine, do not store anything like a model in session, store its id instead, and fetch the corresponding record from the database from the database when access is needed.
See also this chapter about session storage: http://guides.rubyonrails.org/security.html#session-storage

Rails 3 - How to Create a Record if One Does not Exist OR Update the existing records

I'm creating a permissions table for People & Books
In the permissions table I have: Permission.ID, user.id, book.id
I want an admin to be able to set permissions for Users<>Books.
When the user selects the permissions and clicks submit, in the Rails controller, should I be submitting to /create or /update?
is there a way I can submit to just one whether it's new or an update, and let Rails know to either Create or Update a record based on if a record exists per a UserID And BookID?
Thanks
If they're editing an existing record just submit to update. If they are creating a new record and you're making sure there aren't any duplicates, try using find_or_create_by inside your create method.

Resources