I'm trying to figure out how to follow this devise tutorial to add 'approved' to the user model.
https://github.com/plataformatec/devise/wiki/How-To:-Require-admin-to-activate-account-before-sign_in
I have everything set up as shown in the tutorial, except when I save it all and try to click on the link called:
<%= link_to "Users awaiting approval", :action => "index", :approved => "false" %>
I get an error called:
undefined method `find_all_by_approved' for #<Class:0x007fbc5cf34dd0>
It highlights the second line of this users/index action:
def index
if params[:approved] == "false"
#users = User.find_all_by_approved(false)
else
#users = User.all
authorize #users
end
end
Does anyone know what else needs to be done (besides whats shown in the tutorial) to get this functionality to work?
Probably, you had typo issue. Below one should give you same results.
#users = User.where(approved: false)
Related
Another user posts a problem, and I can click on that post to see details about that problem (not through show) and give a recommendation. The thing is that I don't want my recommendation to be linked with this problem. I want it to be linked with that user herself. To do this, I tried:
create
#recommendation = current_user.recommendations.build(recommendation_params)
#user = User.where(user: params[:user_id])
#recommendation.helped_id = #user.id
end
where helped_id should equate that user's id. (later I want that user to be able to see all recommendations she's been given)
But it's turning up error, saying
undefined method `id' for #<User::ActiveRecord_Relation:0x007ff6a9621f68> Did you mean? ids
UPDATE
So I go to the url where this other user's problem is detailed by this view code:
<% #users.each do |u| %>
<%= link_to new_recommendation_path(user_id: u.id) do %>
And the url is: http://localhost:3000/recommendations/new?user_id=2
Could this be the problem?
#user = User.find(params[:user_id]) works fine in new method for showing the problem, but the same code in create method returns cannot find.
You should use the find method which returns the object instead of a relation...
#user = User.find(params[:user_id])
It's possible to continue using AR Relation, so your code would be
#recommendation = current_user.recommendations.build(recommendation_params)
#user = User.where(id: params[:user_id]).first
#recommendation.helped_id = #user.id
Tip: In both scenarios, using User.find or User.where, you should take care of exceptional cases.
Using User.find, if user doesn't exist, then an ActiveRecord::RecordNotFound will be raised.
Using User.where, if user doesn't exist, then a NoMethodError (undefined method `id' for nil:NilClass) will be raised.
The solution was to add nested resources for user and recommendations
resources :users, shallow: true do
resources :recommendations
end
And, in the view, pass both user and recommendation parameters to the recommendation page.
<% #users.each do |u| %>
<%= link_to new_user_recommendation_path(u.id, #recommendation) do %>
Then #user = User.find(params[:user_id]) works.
I've got model Project and model User. I've got belongs_and_has_many in these. But now I need to tell Rails: this specific user belongs to this specific project. How can I do it in Project controller, and how can I call this method from project view? Thank you very much.
in project's*show.html.erb* I ve got:
<select id="user_select" name="user_select" class="input-large">
<% #users.each do |user| %>
<option><%= user.username %></options>
<% end %>
</select>
<!-- button to addfriend method here -->
And I need to call method "addfriend" from here with parameter from selection to associated project with this user :-/
Method addfiend in project controller:
def addfriend
#project = Project.find(params[:id])
#project.users << User.find(params[:user])
respond_to do |format|
format.html { redirect_to project, :notice => 'Added.' }
end
end
This would look something like that in your controller action:
#project = Project.create(:user_id => user_id)
while user_id is your foreign key (something you would probably want to pass from your view).
This code will be written in some controller action, and you would have to define a route for connecting a URL to this action.
Notice that once you call the action that runs this code you can access #project from your view.
You can read about routes here.
You can read about mvc in rails here.
You can read about associations here:
http://guides.rubyonrails.org/association_basics.html
If in model project, you have has_and_belongs_to_many :users, you project object has an implicit collection, users, that can be added to like any other collection, e.g.:
project.users << User.find(:first, :conditions => "name = 'foo'")
I have a method in a helper file that I want activated only when a button is pressed.
def add_f01
#count = Count.find_by_user_id(#user)
#car = Car.find_by_user_id(#user)
#car.toggle!(:f01)
#count.increment!(:v01)
end
How do I do it, please?
I've created a working app here: https://github.com/noahc/stackoverflow
Pull it down and play around with it so you can learn how it works.
Essentially you need the following:
#routes.rb
match 'f01', to: 'users#call_app_controller'
# Anywhere in your view. I have it in index.html.erb of users
<td><%= button_to 'change name', f01_path(user: user)%></td>
#Application controller
def add_f01(user)
user.first = "changed in Application Controller"
user.save
end
#users_controller
def call_app_controller
#user = User.find(params[:user])
add_f01(#user)
redirect_to users_path
end
Using RoR 2.3.8. I have the following codes:
user.rb
def to_param
"#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
end
people_controller.rb
def show
#person = User.find(params[:id])
if current_user == #person
#posts = #person.posts.paginate(:page => params[:page], :order => order)
else
#posts = #person.posts.by_status('published').paginate(:page => params[:page], :order => order)
end
end
I have a column login in Users database where unique username is. People is just a controller to show some posts created by the user.
I will usually link to the index.html.erb under my people controller with the url http://localhost:3000/people/2 with the following code example in User's posts:
<%=h #post.user_name %>
I want the URL to be http://localhost:3000/people/victor where victor is the login for a user. This url should also actually show the profile show.html.erb in people controller.
What else do I need to do? Thanks!
I use the friendly_ID gem for this sort of thing - it's very straightforward - good luck
I would modify routes.rb, something like this:
match 'people/:login' => 'people#show', :as => 'login'
And then modify a people_controller.rb:
def show
#person = User.where(:login => params[:login]).first
end
edited after additional information
corrected error
This seems like a fairly simple problem to me but I have been having some issues.
In one of my views I use something like
<% if current_page?(:controller => "activities", :action => "new") %>
*Do something here*
<% end %>
and it does something specific on the new page for a form. Easy enough and it works great.
Unfortunately, I've found that when you have a "new activity" form (assume normal scaffolding controller), the url will go from
http://localhost:3000/activities/new
after submitting an error prone form to
http://localhost:3000/activities
but it will still show the new activity form with the respective errors. So basically everything works how it is supposed to EXCEPT that I need the url to be http://localhost:3000/activities/new for the current_page? function to recognize that it is indeed a new form page.
I'm wondering if there is some kind of work around to this issue. Thanks!
OH and here is the controller code, in case anybody needs to see it
Controller Code
def new
#activity = Activity.new
end
def create
#activity = Activity.new(params[:activity])
if #activity.save
flash[:notice] = "Successfully created activity."
redirect_to #activity
else
render :action => 'new'
end
end
Think you will need to check for create as well as new
<% if current_page?(:controller => "activities", :action => "new") or current_page?(:controller => "activities", :action => "create") %>
not so pretty maybe wrap it up in a helper method?
You could also check if the created at field is blank. As it won't be set till the activity is created.