codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Ruby on Rails: Association model, do I need controller to save data?


By : , Category : ruby-on-rails

Of course you need a controller. Not necessarily the same controller, but one is needed.

The controller is needed to connect the view and the model. Without it when you submit your data there is no action to send it. Obviously, the database won't be modified this way. You can't even display your view without an action in the controller.

Models without corresponding containers are only used when it is closely attached some other model, like a forum-comment pair. So you can let the user controller to handle business data, but that is not really recommended.

ReLated :

Have you thought about the setup of your models/data structure?

Specifically, duplicating data in one model to another is highly inefficient. You'll be MUCH better using a foreign_key to link your Order model to your cart:

#app/models/cart.rb
class Cart < ActiveRecord::Base
   #stores cart items for processing
   belongs_to :order
   has_many   :cart_items
end 

#app/models/order.rb
class Order < ActiveRecord::Base
   #stores final values (IE delivery service id, cart id, payment id etc)
   belongs_to :user
   belongs_to :delivery
   belongs_to :payment
   has_one    :cart
end

This way, you'll be able to call the following:

#app/controllers/carts_controller.rb
class CartsController < ApplicationController
   def checkout
      @cart  = current_user.cart #-> or however you call it
      @order = @cart.order.new order_params
      if @order.save
         #-> remove cart from session etc etc
      end
   end

   private

   def order_params
      params.require(:order).permit(:delivery_id, :user_id, :payment_id)
   end
end

Obviously very high-level, but quite descriptive -- an Order becomes a glorified join model, allowing you to compile a "cart" with the relative quantities & items, whilst using the Order to manage the delivery and payments etc.

In your User model, in the default_values method, you're creating a new visibility and assigning it to viewable. However, this is scoped such that it is only available to the method, not the class as a whole (and thus does comparison against the method variable).

Also, viewable is the correct usage of the association only in the Visibility model. In the User model, you will have to reference it as visibility.

What needs to be done is to use self to apply the comparison and assignment to the instance variable instead.

def default_values
  self.visibility ||= Visibility.create(default_viewable_params)  
end

use something like

@user = User.create(...)
@projects = params[:projects].map{|k,v| Project.find(k)}
@user.projects << @projects

This looks okay:

@jobapplications = Jobapplication.where("user_id =?", current_user.id)

but not sure about this:

@jobs = @jobapplications.jobs

What's the jobs method?

try this:

#some_controller.rb

def applied_job #note the underscore!
  @jobapplications = Jobapplication.where("user_id =?", current_user.id)
end

and in the view

<% @jobapplications.each do |application| %>
  #list applications here
<% end %>
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .