I created rails3.1 app in spine.js using spine_rails (0.0.9) gem and eco gem. I created two spine:scaffold in my rails app to relate has_many, belongs_to relationship. I included hasMany, belongs_to in spine:model.
I don't know how to get id from one spine:scaffold to another spine:scaffold.
Related
I Created a Model Mobile and Used Device Gem for login . Now i want to associate these two model such that When i Login Using a User I must be able to see only Mobile related to that User
thanks!
Seems you are new with rails. If this is the case, welcome to the world of magic.
As the answer of your question, create a migration file which should add user_id column in mobiles model/table.
Then add
has_many :mobiles to user.rb model file and
belongs_to :user to mobile.rb model file.
In your controller you can fetch all mobiles related to the currently logged in user by
current_user.mobiles
FYI: Here is Official Tutorial of RubyonRails. It will help you a lot.
This is difficult for me to explain, but I need to add the belongs_to attribute to a model of a gem.
I am using the APN_on_rails gem to add push notification functionality to my web app. In this gem it creates a model called Device (APN::Device) which has a few fields needed for it to work.
I now need to link the Device model to my own User model. I have successfully migrated a user_id to the database table.
Now the only problem is adding belongs_to to the model. It is not in die /app/models/ directory.
How can I achieve this?
It's Ruby, so you can just reopen the class and add more to it.
Create a device.rb file in your models directory and just add the appropriate belongs_to as if you created the whole model yourself. As long as you don't redefine any important methods without calling super it will supplement their model with just what you need. You may also need to add an attr_accessible or attr_protected to be able to use it depending on how they defined their model.
I hope that helps.
I heard that Pagination is included automatically for all controllers? How can it be implemented?
As far as I know, there's nothing included automatically. I believe there was built-in pagination in much earlier versions of Rails.
There are (at least) two gems that can be installed to do pagination. The will_paginate gem has been around for years and can be used in Rails 2 or 3. The more recent kaminari gem can be used in Rails 3.
The more recent kaminari gem can be used in Rails 3.
You can refer
https://github.com/amatsuda/kaminari
it is very simple to use.
Put this line in your Gemfile:
gem 'kaminari'
Then do bundle install
Typically, your controller code will look like this:
#users = User.order(:name).page params[:page]
On the views
Just call the paginate helper:
<%= paginate #users %>
Now you can see the paginated result..
As Don Roby said, there are several pagination gems and will_paginate and Kaminari are 2 popular ones. You can find some nice tutorial on pagination (incl. using these 2 gems) here : http://railscasts.com/episodes?utf8=%E2%9C%93&search=pagination
I got the following deprecation warning on the rails console:
DEPRECATION WARNING: Having additional attributes on the join table of a
has_and_belongs_to_many association is deprecated and will be removed in Rails 3.1.
Please use a has_many :through association instead.
The issue lies with the roles_users table that I created following an online step-by-step tutorial.
How do I implement a has_many :through association for acl9? It's beyond me, especially since the user and role models each only use helper methods and no actual has_and_belongs_to_many.
This is how they look like:
class User < ActiveRecord::Base
acts_as_authentic
acts_as_authorization_subject :association_name => :roles
end
class Role < ActiveRecord::Base
acts_as_authorization_role
end
The answer was later discussed in the comments to this GitHub issue.
User model:
acts_as_authorization_subject :association_name => :roles, :join_table_name => :roles_users
Role model:
acts_as_authorization_role :join_table_name => :roles_users
Also, for the record, Rails decided not to deprecate the :join_table option for habtm after all, so this went away with a subsequent patch release of Rails - ie. you shouldn't need the options mentioned in the issue if you just upgrade your Rails.
I'm having trouble with a plugin on a rails 3 app called vote_fu_rails_3. This is a rails 3 compatible version of vote_fu by peteonrails. Both plugins are inspired by acts_as_voteable.
I have a site you can look at http://rubeddit.heroku.com - it's a reddit clone for a class i'm doing.
I am trying to get voting working. I have setup everything according to the instructions on the github page. I have a Link model that has_many :votes and my vote model has belongs_to :user and belongs_to :link.
When trying to do the method to vote_for a link by trying <%= link_to 'up', #user.vote_for(#link) %> I get a SQLite constraint error that voteable_id cannot be NULL.
You can see my code here am i doing something incorrectly? I'm open to any suggestions, i'm pretty new with rails.
Edit: The main issue i'm having seems to stem from voteable_id and voteable_type. These are from polymorphic associations. Since I only have the need to vote on Links (a single model) I guess I have the option of removing the polymorphic associations. Leaving them in place however would also make allowing Comments (yet to be added) to be voted on, fairly simple.