In a rails project, I use devise to manage my user. Now I want remove sign up from login-page and just user that allready exist, can register new user.
What the best way to do this?
To remove the sign up link from the login page, install the Devise Views and remove that link from the "shared/links" partial.
In your terminal, generate the Devise views:
$ cd path/to/app
$ rails generate devise:views users
In the folder views/devise that is created, edit the document shared/_links.html.erb to contain only the links you want to show.
To allow only existing users to create new users, you will want to at least place restrictions on the Devise::RegistrationsController controller actions new and create. This is a bit complex, and requires creating a custom Devise registrations controller.
To do that, create a file in your controller folder called registrations_controller.rb, and update your routes.rb file to point devise_for :users to that new controller. To restrict actions to signed in users only consider using the Devise helper "authenticate_user!". All of those suggestions together might look like this:
#controllers/registration_controllers.rb
class RegistrationsController < Devise::RegistrationsController
before_action :authenticate_user!, only: [:new, :create]
def new
super
end
def new
super
end
end
And your routes
#config/routes.rb
devise_for :users, :controllers => { :registrations => "registrations" }
Ideally, in the views, Users who are not signed in should not even be given the option to try to create new users, so there should be some additional view logic, such as this:
<% if user_signed_in? %>
<%= link_to "New user", new_user_registration_path %>
<% end %>
To clean up your view, you might consider extracting conditional view logic like this into helpers.
With this setup, existing users will be able to create new users through the new_user_registration_path.
For more information, the Devise documentation is pretty useful (https://github.com/plataformatec/devise) as well as perusing other StackOverflow answers.
Related
I am far too new to scopes to figure this out.
I have a list of all users on a page, and I'd like to be able to filter out the list based on role (using a checkbox or link at the top).
I can get this to work with another column in the table (such as location), but I cannot figure out how to do so with roles.
In my model, I have this to make it work for location:
scope :location, -> (location) { where("location LIKE ?", "#{location}%") }
And then in my controller I have
#users = User.location(params[:location]) if params[:location].present?
Which then leads to this in my index
<% #users.each do |user| %>
<%= link_to "filter by #{user.location}", "https://example.com/users?location=" + user.location %>
<% end %>
Of course, switching out with just "role" won't work because Rolify does not keep roles and users in the same table.
I have tried using this in my model
scope :role, ->(role) { Role.find_by(name: role.to_s).users }
but then I cannot figure out how to make it work in my controller and index page.
Any help would be greatly appreciated.
Rolify has a helper method for find users with roles:
User.with_any_role('your_role_here')
The above will return an array with all the users with the specified role
undefined local variable or method `nov_tisk_revij_index' for #<#<Class:0x007f361551b858>:0x007f3615519c60>
Hey guys, I'm setiing up a rails app and can't figure out what's wrong. I used rails g controller nov_tisk_revij index show to create my views and controller and now I can't link to it. If you can spot what I'm doing wrong, I'd be very grateful, don't want to remake this as is took a fair amount of work.
rake routes returns:
Prefix Verb URI Pattern Controller#Action
nov_tisk_revij_index GET /nov_tisk_revij/index(.:format) nov_tisk_revij#index
nov_tisk_revij_show GET /nov_tisk_revij/show(.:format) nov_tisk_revij#show
zic_index GET /zic/index(.:format) zic#index
root GET / zic#index
my html:
<%= link_to 'Novi izvodi tiskanih revij', nov_tisk_revij_index %>
routes:
get 'nov_tisk_revij/index'
get 'nov_tisk_revij/show'
get 'zic/index'
I tried adding resources :nov_tisk_revij into routes, and it still didn't work, just created additional routes for views I do not have.
controller:
class NovTiskRevijController < ApplicationController
def index
end
def show
end
end
I'm using rails4 with ruby 2.
<%= link_to 'Novi izvodi tiskanih revij', nov_tisk_revij_index_path %>
works
In my project I am dealing with objects called Workflows. Every Workflow can have many subflows (Workflows that it calls). At the same time every workflow (Including ones with sub flows) may also act as a Superflow that is calling the sub-flows and may have many Superflows that call it as a sub flow.
I am currently trying to do this by using a join table through a model called FlowRelation.
I have followed every tutorial I can find and read every article I can find and I still can't get this working.
Workflow Model:
class Workflow < ActiveRecord::Base
belongs_to :superflow, :class_name => 'Workflow'
has_many :subflows, :class_name => 'Workflow', :foreign_key => 'subflow_id'
end
FlowRelation Model:
class FlowRelation < ActiveRecord::Base
belongs_to :workflow
belongs_to :flow, :class_name => "Workflow"
end
Inside my workflows edit.html.erb (Using SimpleForm)
<%= simple_form_for #workflow, :html => { :class => 'form-horizontal', :multipart => true } do |f| %>
<%= f.input :workflow_id %>
<%= error_span(#workflow[:workflow_id]) %>
<%= f.association :subflows, :include_blank => t('.select_workflow') %>
<%= error_span(#workflow[:subflow_id]) %>
<%= f.association :superflow, :include_blank => t('.select_workflow') %>
<%= error_span(#workflow[:superflow_id]) %>
And my Workflow Controller is pretty straightforward
def new
#workflow = Workflow.new
end
def edit
#workflow = Workflow.find(params[:id])
#workflow.save
end
Currently, when I attempt to save new sub_flows to an existing or new Workflow, nothing ever gets saved. Nothing shows up in the database, and I see no error.
I also have a FlowRelationsController
class FlowRelationsController < ApplicationController
before_action :set_flow_relation, only: [:show, :edit, :update, :destroy]
def create
#flow_relation = current_workflow.flow_relations.build(:flow_id => params[:flow_id])
if #flow_relation.save
flash[:notice] = "Added flow."
redirect_to root_url
else
flash[:error] = "Unable to add flow."
redirect_to root_url
end
end
If I attempt to create an add sub_flow button to my workflow index with
<%= link_to "Add Subflow", flow_relations_path(:flow_id => workflow), :method => :post %>
My flow_relations table remains empty as well. and I get the following error:
undefined local variable or method `current_workflow' for #
<FlowRelationsController:0x007f70154921e0>
I know its a bunch of pieces, but I'm completely stuck, been hitting my head against a wall on this one for days. Anyone see something I'm missing here? I've been trying to implement something similar to what was described on RailsCasts here with no success: http://railscasts.com/episodes/163-self-referential-association?view=asciicast
Please HELP!
Thanks
Without digging into any of the other self-referential model stuff you're doing here, the error you see is a controller error. It doesn't know about 'current_workflow'. My guess is that in your 'set_flow_relation' before callback, you are setting #current_workflow. I cannot be sure without seeing the rest of your controller code.
Once that is fixed, please post any follow up problems you have.
I was misunderstanding a couple different things. I thought I'd answer my own question to help bring clarity to any others who may be in the same boat.
1st major issue I had above was in the FlowRelationsController. I was using an object called current_workflow which didnt exist. This happened due to following the only video i could find online about this topic at: http://railscasts.com/episodes/163-self-referential-association. This is a create resource, but is not entirely the same as what I was trying to accomplish. The current_workflow object wasnt being created in my case because I was starting from an index and not a workflow details section of my application
Now, as to how to accomplish my original goal. Here is what I ended up with:
Workflow Model
has_many :flow_relations
has_many :subflows, :through => :flow_relations
FlowRelation Model
self.primary_key = :workflow_id
belongs_to :workflow
belongs_to :subflow, :class_name => 'Workflow'
Inside of my simpleForm used for creation and editing of workflows. (This was a pretty key piece to understand. - doing the association to subflows with a collection of all workflow objects.
<%= f.association :subflows, collection: Workflow.all, prompt: "Select Subflows" %>
<%= error_span(#workflow[:subflow_id]) %>
The Workflow controller stayed mostly the same with one key difference due to Rails 4. - I had to change the workflow_params to permit!. I did this on both the Worfklow Controller and the FlowRelations controller. Not sure I understand still how this should be configured for better security, but in this particular application, security is not an issue as its to be run internally.
def new
#workflow = Workflow.new
end
def edit
#workflow = Workflow.find(params[:id])
#workflow.save
end
def workflow_params
#params.require(:workflow).permit(:workflow_id, :flow_relations, :re_workflow_id, :name, :description, :superflow_id, :client_id, :variable_id, :required_variable_id, :flow_file)
params.require(:workflow).permit!
end
I removed the add subflow button, since now it could be done through the standard create or edit actions
Its a bunch of stuff, but basically, the end result is I now have a Workflow object that can contain mnay subflows all of which can be a member of many Workflows, all within a single Workflow class and a FlowRelations class for handeling the join.
I have an edit action that I would like to put on a modal window on my App, and when it's closed I need to refresh a part of my view.
Does anybody knows how can I do that using Rails 4?
Lets assume you want to open the user edit page in a modal. Here's the solution I prefer:
# Create a ajax link somewhere that opens the user modal:
<%= link_to user.name, edit_user_path(user), remote: true %>
# This link calls your controller action:
class UsersController < ApplicationController
def edit
#user = User.find(params[:id])
render_js_modal(:any_id, 'edit')
end
end
class ApplicationController
def render_js_modal(id, partial, options)
#id = id
#partial = partial
#options = options
respond_to do |format|
format.js { render 'application/render_modal' }
end
end
end
# The view depends on which CSS library you are using. This
# example is if you use semantic ui. If you use something like
# bootstrap you will probably need to create a bit more html in the JS.
# views/application/render_modal.js.erb:
$(body).append('<div id="<%= #id %>"></div>');
var element = $('#<%= #id %>');
element.html('<%= j(render partial: #partial, #options) %>');
element.modal('show');
Hope this helps. If you get any errors, remember to look in the rails server log for details. For JS details look in the browser console. Note you can pass locals to the partial in the options param. You can create many other useful methods like this:
render_js_modal_hide: hide a modal on screen by id. Useful when the user is created successfully. Consider replacing the page flash messages with the JS response as well.
render_js_content_replace: replaces a div on the page with the content of provided partial. This is useful for replacing the content of a modal if there are validation errors.
etc.
I've done this in that way (using Slim):
.text-center = link_to 'Visualizar Documentos', 'Visualizar Documentos', class: 'btn btn-default btn-primary btn-large', "data-toggle" => "modal", "data-target" => "#meioambiente"
.modal.fade id="meioambiente" tabindex="-1" role="dialog" aria-labelledby="meioambiente-label" aria-hidden="true"
And everything worked perfectly. Thanks!
Twitter Bootstrap and ZURB Foundation both have modal components. You will need to use JavaScript to update the DOM.
Codecademy is a free online learning website and have great tutorials to learn JavaScript and jQuery. I would also suggest Code School since they have a free course on JavaScript, jQuery and Angular.js.
In my app I have Issue model, which belongs_to Status model. What I need to do is to have dropdown list of links (or spans, doesn't matter) with Statuses Id's on Issue show page, so I could change status clicking on this (could be non-ajax and ajax).
I am rather new to Rails, so I do not know how to implement this, was thinking of few ways to do this, but non seems to be working.
Because you're new, I'll outline what I'd do for you (hopefully it will help):
#config/routes.rb
resources :issues do
get "change_status/:status_id", to: "issues#change_status"
end
#app/models/status.rb
Class Status < ActiveRecord::Base
has_many :issues
scope :not, ->(id) { where('statuses.id != ?', id) }
end
#app/controllers/issues_controller.rb
def show
#issue = Issue.find params[:id]
#statuses = Status.all.not(#issue.id)
end
def change_status
issue = Issue.find params[:id]
issue.status = params[:status_id]
issue.save if issue.status_id_changed?
redirect_to issue
end
#app/views/issues/show.html.erb
<%= #statuses.each do |status| %>
<%= link_to status.title, issue_change_status_path(#issue, status) %>
<% end %>
There's obviously some stuff to explain - if you need me to do that, let me know and I'll give you the details!
You can simply use the link_to rails helper like in the following code:
link_to "Change Status", my_path_to_update_status, remote: true, method: :put
This will send an AJAX PUT request (thank to the remote: true) to your my_path_to_update_status.
Then, you just have to update the status in the action corresponding to the path specified in the link_to helper tag.
More information about the link_to rails helper here