codingdir logo sitemap sitemap |

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:

class Cart < ActiveRecord::Base
   #stores cart items for processing
   belongs_to :order
   has_many   :cart_items

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

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

class CartsController < ApplicationController
   def checkout
      @cart  = current_user.cart #-> or however you call it
      @order = order_params
         #-> remove cart from session etc etc


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

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)  

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 =?",

but not sure about this:

@jobs =

What's the jobs method?

try this:


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

and in the view

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

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 © All Rights Reserved .