Adding Categories In Rails 3 - ruby

I am building an uploader for videos and giving the videos a category through categorizations. Every time I try to upload a video I receive an error saying
NameError in VideosController#create
uninitialized constant Video::Categorization
I want to able to add one category to each video. But no regardless of how I write the association I get the same error.
model
class Video < ActiveRecord::Base
attr_accessible :source, :title, :url, :description,
:category, :category_id, :category_list
belongs_to :user
has_many :category, through: :categorizations
has_many :categorizations
validates :category, presence: true
has_attached_file :source
def source_remote_url=(url_value)
self.source = URI.parse(url_value) unless url_value.blank?
super
end
def self.categorized_with(name)
Category.find_by_name!(name).videos
end
def category_list
["Action", "Anime",
"Arts and Culture", "Beauty", "Business", "Comedy",
"Documentary", "Drama",
"Food", "Gaming", "Health and Fitness", "Horror"]
end
def category_list=(names)
self.category = names.split(",").map do |n|
Category.where(name: n.strip).first_or_create!
end
end
end
Video Controller
class VideosController < ApplicationController
before_filter :authenticate_user!
before_filter :set_video, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#videos = Video.all
end
def show
respond_with(#video)
end
def new
#video = Video.new
respond_with(#video)
end
def edit
end
def create
#video = Video.new(params[:video])
#video.save
respond_with(#video)
end
def update
#video.update_attributes(params[:video])
respond_with(#video)
end
def destroy
#video.destroy
respond_with(#video)
end
private
def set_video
#video = Video.find(params[:id])
end
end
Form
<%= form_for(#video) do |f| %>
<% if #video.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#video.errors.count, "error") %> prohibited this video from being saved:</h2>
<ul>
<% #video.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :source %><br />
<%= f.file_field :source %>
</div>
<div class="field">
<%= f.label "Category", class: 'control-label' %>
<%#= f.select :category, Category.all, :prompt => 'Select One' %>
<%= f.select :category_list, video_category, :prompt => "Select a category..." %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description, rows: 4, placeholder: "Description" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

If you declare
has_many :category, through: :categorizations
it should be
has_many :categories, through: :categorizations

Related

Generate current session's user id into the form in rails

I have two tables Users and Incomes. User has an id column and Income has a foreign key of user_id that is referencing User's id column. The association has been created in the model: User will has_many incomes and income belongs_to user. I want the new.html.erb in the income model to submit a new income item and set its foreign key to the session's current user's id without the user put in id in the form(that means user must sign in with his/her account).
Here is my application controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user #make the private method to be accessible for the view
end
end
Here is my new.html.erb for the income model:
<h1>Add New Income</h1>
<div class="field">
<%= form_tag %>
<%= label_tag :title %>
<%= text_field :title, params[:title] %>
<%= label_tag :amount %>
<%= number_field :amount, params[:amount] %>
<br>
<% if current_user %>
User ID:<%= current_user.id %>
User Name: <%= current_user.user_name %>
<% :user_id = current_user.id%>
<% end %>
<%= submit_tag "Submit" %>
Try
<%= form_for(#income, :html =>{:class => "form "}) do |f| %>
<%= label_tag :title %>
<%= text_field :title, params[:title] %>
<%= label_tag :amount %>
<%= number_field :amount, params[:amount] %>
<br>
<% if current_user %>
User ID:<%= current_user.id %>
User Name: <%= current_user.user_name %>
<%= f.hidden_field :user_id, value: current_user.id%>
<% end %>
<%= submit_tag "Submit" %>
<% end %>

validations failing as blank when fields not blank

I am runnning ruby 1.9.3 and rails 4.1.4
trying the following validations
in the models\profile.rb
attr_accessor :password
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :password, :confirmation => true #password_confirmation attr
validates_length_of :password, :in => 6..20, :on => :create
before_save :encrypt_password
in view profiles\new.html.erb
from the form
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.label :interests %><br />
<%= f.text_area :interests %>
</p>
<p>
<%= f.label :zip %><br />
<%= f.text_field :zip %>
</p>
<p>
<%= f.label :country %><br />
<%= f.country_select :country, ["United Kingdom"] %> </p>
using strong parameters controllers\profile_controller,rb
class ProfilesController < ApplicationController
def new
#profile = Profile.new
end
def create
#profile = Profile.new(params[profile_params])
if #profile.save
flash[:notice] = "You signed up successfully"
flash[:color]= "valid"
else
flash[:notice] = "Form is invalid"
flash[:color]= "invalid"
end
render "new"
end
private
## Strong Parameters
def profile_params
params.require(:profile).permit(:name, :email, :password, :interests,:zip)
end
end
validations always fail saying fields are blank. By causing an exception the report shows model profile with its fields populated. It is as if the data is simply not accessible so I suspect I'm misuing strong parameters sonehow.
Any opinions welcomed.
The profile_params function is going to return a hash for you with the data directly.
What that basically does is it filters the params hash for the 'profile' key's value for the keys named as the parameters of permit(...)
So the way to fix is to write
#profile = Profile.new(profile_params)
instead of
#profile = Profile.new(params[profile_params])
With strong parameters you almost never interact with the params array directly from controller actions (such as create). The private strong parameter methods will handle that for you and by that, only safe, filtered data will be given to your controller actions.

Can not create user with Rails app on heroku, but works perfect on localhost

So I am working through the Michael Hartl tut and this app works perfectly on the localhost but the moment I deploy to heroku it wont create a user when i submit the information. In fact it just sits there as if I just clicked on an empty screen, no error message nor a rediret. I looked at the heroku logs and there are no exceptions that I can see being logged. I tried updating the controller behavior but i get the same result. This is frustrating.
my form looks like this:
<div class="main-form">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages'%>
<%= f.label :name %>
<%= f.text_field :name, class: "active" %><br/>
<%= f.label :email %>
<%= f.text_field :email %><br/>
<%= f.label :password %>
<%= f.password_field :password %><br/>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %><br/>
</div>
<div class="actions">
<%= f.submit "create my account", class: "btn btn-lg btn-default" %>
</div>
<% end %>
my controller looks like:
class UsersController < ApplicationController
def new
end
def show
#user = User.find(params[:id])
#title = #user.name
end
def create
#title = "welcome"
#user = User.new(user_params)
if #user.password_confirmation.empty? == false
#user.save
redirect_to user_path(#user)
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
i have also tried setting up my create method like:
def create
#user = User.new(user_params)
if #user.save
redirect_to #user
else
render 'new'
end
end
neither of these methods worked? Any advice would be welcome.
If you inspect the page it looks like your submit button is outside of the form definition:

Not saving on the database from nested form

I want to create a dummy account for a new User object. Whenever a user signs up in my app it should automatically create an account for him.
But what's happening is, I'm saving in the database the new User, but not the new Account. I'm also not aware what's the best way to respect the MVC pattern design when it comes to this situation. I'm afraid of replicating code or to have one controller doing the work of two.
The sign up form, has nested resources (and it's build on top of devise)
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :name %><br />
<%= f.text_field :name %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<p>Account name</p>
<%= f.fields_for :account do |builder| %>
<fieldset>
<%= builder.label :title %>
<%= builder.text_field :title %>
</fieldset>
<% end %>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
My User Model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :accounts
accepts_nested_attributes_for :accounts
end
and my Account Model:
class Account < ActiveRecord::Base
belongs_to :user
end
My User Controller:
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
sign_in #user
##flash[:success] = "Welcome to the HighTide!"
redirect_to #user
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation, accounts_attributes: [:id, :title])
end
end
and finally the Account Controller:
class AccountsController < ApplicationController
def new
#account = #user.accounts.new
end
def create
#account = current_user.accounts.new(account_params)
if #account.save
redirect_to '/'
end
end
private
def account_params
params.require(:account).permit(:title, :user_id, products_attributes: [:id, :title, :units], bookings_attributes: [:id, :name, :check_in, :check_out])
end
end
EDIT:
routes.rb file
Hightide::Application.routes.draw do
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}
resources :users do
resources :accounts
end
resources :sessions
match '/users/:user/edit', to: 'users#edit', via: [:post, :get]
devise_scope :user do
root to: 'static_pages#home'
match '/sessions/user', to: 'devise/sessions#create', via: :post
end
Your form_for can only send its data to a single controller method, and in this case it is sending the params from your form to the Devise registrations controller. Those params include the values for the new account, but they never reach your users#create or accounts#create methods.
You'll probably have to create a custom Devise controller. Take a look at this SO question and answer, which lays it out nicely. Nested registration data in Rails 3.1 with Devise.

how to do a many to many model for this project in rails

I'm having trouble creating a many to many model for my project.
Basically i have a Matches & Teams model.
Teams are created prior to the Matches.
Once the match is created then i would like to add teams to it.
Match can have many teams, Teams can have many matches.
I'm currently adding teams via nested_form and adding multiple teams at once.
When submitting the form, i get an error expecting the team to be in a relationship already with the match.
I can do this with a many to one relationship but it fails with many-to-many, was wondering if there was any way to do it without doing a custom route.
Below is the form, controllers are as per default values.
Form:
<%= nested_form_for(#match) do |f| %>
<% if #match.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#match.errors.count, "error") %> prohibited this match from being saved:</h2>
<ul>
<% #match.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<%= f.fields_for :teams, :html => { :class => 'form-vertical' } do |builder| %>
<%= builder.label "Team Name:" %>
<%= builder.autocomplete_field :name, autocomplete_team_name_teams_path, :update_elements => {:id => "##{form_tag_id(builder.object_name, :id)}" },:class => "input-small",:placeholder => "Search" %>
<%= builder.hidden_field :id %>
<% end %>
<%= f.link_to_add raw('<i class="icon-plus-sign"></i>'), :teams, :class => 'btn btn-small btn-primary' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Using a join model, the has_many :through macro and the accepts_nested_attributes_for macro you can do something like the following.
class Match
has_many :competitions
has_many :teams, :through => :competitions
accepts_nested_attributes_for :teams
end
class Competition
belongs_to :match
belongs_to :team
end
class Team
has_many :competitions
has_many :matches, :through => :competitions
end
Just make sure your form is set up to send the following data structure as params when the request reaches the create or update controller.
params => {
:match => {
# ...
:teams_attributes => [
{ :name => 'Foo', :color => 'blue' },
{ :name => 'Bar', :color => 'green' },
# ...
]
}
}

Resources