I have a problem with my application, crash when I put the path student_postulations_path in the navbar :S.
I have in my route.rb
resources :students do
resources :postulations
end
and my application say this error
No route matches {:controller=>"postulations"}
My view
<% elsif student_signed_in? %>
<% menu_group :pull => :right do %>
<%= menu_item "Postulaciones", student_postulations_path %>
<%= menu_divider %>
<%= drop_down current_student.email do %>
<%= menu_item "Logout", destroy_student_session_path, :method => :delete %>
<% end %>
<% end %>
<% else %>
when I use rake routes:
student_postulations GET /students/:student_id/postulations(.:format) postulations#index
POST /students/:student_id/postulations(.:format) postulations#create
new_student_postulation GET /students/:student_id/postulations/new(.:format) postulations#new
edit_student_postulation GET /students/:student_id/postulations/:id/edit(.:format) postulations#edit
student_postulation GET /students/:student_id/postulations/:id(.:format) postulations#show
PUT /students/:student_id/postulations/:id(.:format) postulations#update
DELETE /students/:student_id/postulations/:id(.:format) postulations#destroy
My models student and postulation:
class Student < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :nombre, :rol,
:prioridad, :resumen, :categoria, :foto
# attr_accessible :title, :body
has_many :postulations
end
class Postulation < ActiveRecord::Base
attr_accessible :status, :student_id
belongs_to :student
end
I dont understand my error... I thing have all right. Please help! :).
Thank
In nested resource routing you are getting
student_postulations GET /students/:student_id/postulations(.:format)
So in your view you need to pass the #student object with student_postulations_path(#student)
The helpers like student_postulations_path, student_postulations_url take an instance of Student as the first parameter student_postulations_path(#student) to find the record of student .
You can get more info from the guide
So your link path will be
<%= menu_item "Postulaciones", student_postulations_path(current_student) %>
Related
I am building a login system that has two users, buyer and seller, in Rails 4.0.4.
For auth I am using the Devise gem: https://github.com/plataformatec/devise
To create a new buyer I use the route buyer/new. However, the fields for the user do not show in the view. I also using debug to show #buyer.user in the view and it has been created. But when I call f.fields_for #buyer.user do |u| the loop is never entered.
Any ideas of why this is? Also, the polymorphic associations seem to be working in the rails console.
Buyer Controller:
# GET /buyers/new
def new
#buyer = Buyer.new
#buyer.build_user
end
Buyer Model
class Buyer < ActiveRecord::Base
has_one :user, as: :role
accepts_nested_attributes_for :user
end
Buyer/new View
<%= form_for(#buyer) do |f| %>
....
<div class="field">
<%= debug(#buyer.user) %>
<% f.fields_for #buyer.user do |u| %>
<%= u.text_field :email %>
<% end %>
</div>
User Model
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :role, polymorphic: true
Shouldn't you have an = on the fields_for? http://rubydoc.info/docs/rails/ActionView/Helpers/FormHelper:fields_for
E.G.
<%= f.fields_for #buyer.user do |u| %>
I'm having an error with a form in a view, can't get it why is happening. I keep getting ActionView::Template::Error (undefined method 'stage' for #<User:0x007f80045ca0e0>)
I have two models, User and Stage. User has_many stages, and stages belongs_to to user. It's as follows
The Stage Model:
class Stage < ActiveRecord::Base
belongs_to :user
end
The User Model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :user_setting
has_many :matches, dependent: :destroy
has_many :stages, dependent: :destroy
end
and in the user controller I have as follows:
class UsersController < ApplicationController
def show
#newMatch = current_user.matches.new
#newStage = current_user.drivepipes.new
end
end
and a form on the show.html.erb
<%= form_for [current_user], url: user_stages_path(current_user, #newStage) do |s| %>
<%= s.text_field :stage, placeholder: "Stage" %>
<%= s.submit "Save" %>
<% end %>
If each user has many stages and you're making a form for your user, <%= s.text_field :stage, placeholder: "Stage" %> is going to give you an error because the user has_many :stages which is an enumerable. So you're going to want something like
<%= form_for [current_user], url: user_stages_path(current_user, #newStage) do |s| %>
<% current_user.stages.each do |stage| %>
<%= s.text_field stage, placeholder: "Stage" %>
<% end %>
<%= s.submit "Save" %>
<% end %>
Assuming you want a text field for every stage the user has, that is. Perhaps that's not the goal here?
There is one-to-many relationship between User and Stage. So, foreign key will reside in Stage table i.e. user_id. You should make clear that You can't have an attribute 'stage' or stage_id in User object if it has many stages. However Rails have helpers:
#user.stages
will return array of stages which have been saved with this user's id, using query:
select * from stages where stages.user_id = #user.id
While:
#user.stage
will raise error.
If you want to get input for newly created stage object associated with current user, You view code should look like this:
<%= form_for current_user, url: user_stages_path(current_user, #newStage) do |user_form| %>
<%= user_form.fields_for #newStage do |stage| %>
<%= stage.text_field :stage, placeholder: "Stage" %>
<% end %>
<%= user_form.submit "Save" %>
<% end %>
Later, in controller; you can get the stage object in the similar way:
stage = Stage.new params[:user][:stage]
I have Account model which have a has_many relationship with User model:
class Account < ActiveRecord::Base
has_many :users, -> { uniq }
accepts_nested_attributes_for :users
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :confirmable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :account
I added avatar attribute to User model using paperclip.
I want each user to have access to the common account settings, and inside it having the possibility to upload his/her own avatar.
I use simple_form so I tried this:
<%= simple_form_for current_account, :html => { :multipart => true } do |f| %>
<%# here come account settings %>
<%= f.input :time_zone, :label => t(".timezone"),
:
:
<%# here I need to access current user attributes %>
<%= f.simple_fields_for :user, current_account.users.first do |user_form| %>
<%= user_form.file_field :avatar, :error => false %>
<% end %>
<% end %>
First problem:
I need some logic to access current_user instead of current_account.users.first. Since there is a superadmin which can access all accounts, use current_user is not enough.
Second (and bigger) problem:
I added in my controller the avatar parameter to the whitelist:
def allowed_params
params.require(:account).permit(:time_zone, :logo, :description, user: [:avatar])
end
When I try to update my model:
if current_account.update(allowed_params)
I get this error:
unknown attribute: user
I also tried:
params.require(:account).permit(:language, :time_zone, :logo, :description, :user_attributes => [:avatar])
and:
params.require(:account).permit(:language, :time_zone, :logo, :description, :users_attributes => [:avatar])
(in plural)
but since I use ActionController::Parameters.action_on_unpermitted_parameters = :raise I get:
found unpermitted parameters: user
It must be something very easy, some help please?
Ok, got it!!
The problem is the one-to-many relationship and the way I tried to access a single instance of user. The correct way to do it is:
<% current_account.users.each_with_index do |user, index|%>
<%= f.simple_fields_for :users, user do |user_form| %>
<%= user_form.file_field :avatar, :error => false %>
<% end %>
<% end %>
As you can see, the iteration should be done over the relation, and only when having a
single instance "in hand" we can user the simple_fields_for.
Also, notice that the first parameter passed to simple_fields_for is :users and not :user, since this is a one-to-many relationship.
I have a friendship model that is working but when I include this code in my user/show view, I get this unwanted result:
Friends
Dave Olson, accepted
[#<Friendship id: 74, user_id: 1, friend_id: 2, status: "accepted", created_at: "2014-03-27 03:54:08", updated_at: "2014-03-27 03:54:09">]
I can't figure out why the extra Hash prints out.
Here's the code from my view:
<h3>Friends</h3>
<%= #user.friendship.each do |friendship| %>
<p><%= friendship.friend.name %>, <%= friendship.status %></p>
<% end %>
The User model is:
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_many :items
has_many :friendship
end
And the relevant portion of my Friendship Model:
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: "User", foreign_key: "friend_id"
validates_presence_of :user_id, :friend_id
....more code
end
The only way I have been able to eliminate the hash is by not running the block. Unfortunately that isn't going to work. So, why is the hash printing out? I have tried searching for an answer but haven't had any success. Any help would be greatly appreciated.
Remove the equals sign = from the erb scriptlet used in the each loop:
<h3>Friends</h3>
<% #user.friendship.each do |friendship| %>
<p><%= friendship.friend.name %>, <%= friendship.status %></p>
<% end %>
The reason you see the unwanted hash is because <%= as opposed to <% is used to print the output. So, <%= #user.friendship.each... prints the result returned by that each block.
Simply put
This tag will output something
<%= ... %>
This tag will not
<% ... %>
I have a Post model:
class Post < ActiveRecord::Base
attr_accessible :title, :content, :tag_names
belongs_to :user
has_many :comments, :dependent => :destroy
end
belongs_to :post, :counter_cache => true
belongs_to :user
end
a User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :avatar, :subscribed_tag_names
has_many :posts, :dependent => :destroy
has_many :comments, :dependent => :destroy
end
and a Comment model:
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :post, :counter_cache => true
belongs_to :user
end
This is how I show the user who created the post in the index.html.erb view:
<% #posts.each do |post| %>
<div id="post-<%= post.id %>" class="post">
<h3 class="post-title"><%= link_to post.title, post %></h3>
<div class="post-author">
<span class="profile-picture">
<%= image_tag post.user.avatar.url(:thumb) %>
</span>
<span class="post-author-name">
<strong><%= link_to post.user.username, post.user %></strong>
</span>
</div>
(etc...)
How to display the user who last commented the post (as you can see in StackOverflow and various forums)?
<% comment = post.comments.order(:created_at).reverse_order.first %>
<%= comment.user.email if comment %>
This does the following:
it get all comments of the post, ordered by the created_at field in reverse order (i.e. the largest value on top). From that, it selects the first value, i.e. the newest comment. From that comment, you then get the user.
Setting the order is non-optional as databases are free to return elements in an arbitrary order if it is not specified explicitly. You will observe the random order more often on Postgres or Oracle than on MySQL or SQLite because of the way they store records. However, all of them will return random element orders at least occasionally if the order is not specified.
The previous answer only works when your post has some comments. When there are no comments then you will receive an error, as you have noted.
You could solve this by simply testing that comments exist before trying to output it:
<% #posts.each do |post| %>
...
<% if post.comments.empty? %>
Nobody has commented yet
<% else %>
<%= post.comments.last.user.email %>
<% end %>
<% end %>
<% #posts.each do |post| %>
...
<%= post.comments.last.user.email %>
<% end %>