How to create an instance of a model upon button press - ruby-on-rails

Let's say I have a User, Poll, and Vote model.
My user is created at registration and polls are created via a form the user has access to.
Upon showing a particular poll to a user, how can I create a new instance of the vote model when the user clicks a poll option?
Basically I want to create a new vote model instance when the user clicks a button that passes the current user_id and poll_id as parameters as well as a value for the option they selected.
My vote model 3 attributes: poll_id, user_id, and value.
poll_id and user_id are foreign keys to the poll and user tables, and value just records the chosen poll option.
I attempted to try and achieve this by calling Vote.create upon selecting the button like this:
<a class="btn-floating btn-large waves-effect waves-light hoverable red left" href="<%= poll_path(#next) %>" >
<i class="material-icons">arrow_back</i>
<%= Vote.create %>
</a>
This is how I'm getting access to the poll_id and user_id inside of my vote create method inside the vote controller
#current_user ||= User.find(session[:user_id]) if session[:user_id]
#vote.user_id = #current_user
#current_poll ||= Poll.find(session[:poll_id]) if session[:poll_id]
#vote.poll_id = #current_poll
Basically, I'm wondering what the correct rails-like way would be to go about this.
Disclaimer: I am very very new to rails and realise that I may be posing this question incorrectly!

You don't do Vote.create in the view. You do that in the controller.
Create a button using the appropriate helper in your .erb file, the vote_path, and passing in poll_id, user_id, and value. That will give you a button in your view that, when clicked, will POST to the create action on your VoteController where you will do the object instantiation and save. From there you do a redirect to where ever is appropriate given the results of the create action.
You'll need to go back and read up on how Rails uses ERB to construct HTML files for rendering in the browser. Your question suggests you haven't really groked it yet.

This solved my issue:
<td><%= button_to '<' ,{:controller => 'votes', :action => 'create', :user_id => 1, :poll_id => #poll.id, :value => 0}, {:method => :post} %></td>

Related

How to attach an action call to a submit button in Rails?

I have a page where a deactivated user account can be activated - but before they can be activated, they need to be assigned a new password. Currently I have two buttons to perform these actions:
At the end of the 'add password' form there is this button:
<%= f.submit %>
A little further on the page there is this button, which sets the status of the user to 'active':
<%= standard_button 'Activate user', update_status_user_path(#user),
:method => :patch %>
If possible, I'd like add the functionality of the second button to the submit button.
I don't fully grasp the magic behind forms and their buttons, so I am wondering whether this is possible and if so, how I could accomplish this?
You can think of a button as a mini form. View the docs to get more information about the button_to rails method.
In your form_for, there will be a url which specifies where the form is posting to. In that controller method, you can include the logic to update the user's status to 'active'. Something like
def some_method
user = User.find(params[:user_id])
user.update_attribute(:status, "active")
end

Automatically populating simple_form fields - RAILS

Let's say I have a Customer and a Brand class (where a customer has many brands and a brand belongs to a customer). When the user is on the show page for a specific customer, he/she has the option to click a button to add a new brand (that button redirects to the form to create a new brand, which contains a field where the user should indicate which customer that brand belongs to). But when the user gets to that form, he/she should not have to manually indicate which customer that brand belongs to, but rather that information should be entered automatically based on the customer_id of the customer whose show page the user was just looking at. If no show page was being looked at, the field for the parent customer_id should be empty (as it is by default) and the user should enter it manually.
Is there a way to implement this in Rails? If so, how?
Your question is a bit hard to understand, but let's go.
First, you have to create a link to a new brand, something like:
<%= link_to 'New brand for #{#customer.name}", new_brand_path(customer_id: #customer.id) %>
In this way you are passing the customer_id as a param.
In the brand controller on the new action you will do
def new
#customer = Customer.find_by_id(params[:customer_id])
#brand = #customer ? Brand.new(customer_id: #customer.id) : Brand.new
end
You see that I made an example to make it clear what to do. There are better ways of doing that, but I guess this will guide you through what you want.
On the link from the show page, you could have something like this
<%= link_to 'New Brand', new_brand_path(customer_id: params[:id]) %>
Then in the controller, you could have some code that checks to see if the customer_id is present in the params hash in the new action:
#brand.customer = Customer.find(params[:customer_id]) if params[:customer_id]
Now, when the form is rendered, assuming using form_for #brand, the customer field will be set.
If they are linking from another page (other than show), then just don't pass in the customer_id: params[:id] in the link_to method call. It will bypass the if statement in the controller and the customer will be blank.
#new_brand = #customer.brands.new
It populates cusomer_id in #new_brand with correct value

how form_for works in Ruby on Rails

I am an newbie. I have read the API documentation. But still don't understand how form_for works.
Firstly, from Ruby on Rails Tutorial, the form for follow button:
<%= form_for(current_user.relationships.build(followed_id: #user.id)) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>
I understand current_user.relationships.build(followed_id: #user.id) means a new record. But why can we not just submit and trigger controller to save the record without hidden_field? Why do we still need to post followed_id to controller?
Secondly, in hidden_field, what does :followed_id means? I believe that is a symbol, i.e. it equals only "followed_id" not a variable of id. If that is only the name of the input field, then what is its value?
Thirdly, how does form_for know where the submission should be sent to? Which controller and action the form_for will post to?
Fourth, how does params work with form_for? In this follow button case, params[:relationship][:followed_id] will return #user.id in controller. How does it know the first hash attribute is :relationship? We have neither mentioned form_for :relationship nor form_for #relationship.
I know these questions can be very dumb, but I am really stuck. Any help will be appreciated.
I didnt do that tutorial so mind me if i dont answer directly to your question.
Take a look at the rails guide about form helpers and it explains in details your questions, probably in a more articulate way than i can.
form_for(path/to/your/controller/action) is a helper method to create HTML form elements with the url path to the POST or GET request. The helper knows if it should be a new record or an update record based on what you are asking to do in your controller action.
For example
In your controller
def new
#my_instance_variable = Myobject.new
end
In your view new.html.erb
<%= form_for #my_instance_variable do |f| %>
...
<% end %>
In your case the logic was directly written in the helper and you could also directly write
<%= form_for Myobject.new %>
Both will result with the following html
<form action="/myobjects/new" method="post">
# in this case rails knows its a `POST` request because the route new action
# is by default a POST request. You can check these routes and their request
# by using `rake routes` in terminal.
Then the hidden_field is another helper to contain a value, in your case the #user.id that will be passed as parameter then saved as a Create or update action for the given object. The reason it doesnt add the value in the hidden field tag is because you already have a model association that knows the id of user since the link of form uses the build method with user id.
Last part you need to understand the form_for link logic
current_user.relationships
# implies the association of the current_user has many relationships
current_user.relationships.build
# .build is a method to populate a new object that can be save as a new record
# means you will create a new relationship record by populating the user_id
# column with the current_user.id and the followed_id with the target #user.id
After reading the book The Rails 4 Way, I understand form_for better now.
11.9.1.5 Displaying Existing Values.
If you were editing an existing instance of Person, that object’s attribute values would have been filled into
the form.
in this way, when we build the relationship by usingcurrent_user.relationships.build(followed_id: #user.id), the relationship instance will be created and gain attribute followed_id. So that, instead of "creating" a relationship, we are actually editing the relationship by the form.
Then Rails will know you are editing and load the existing attribute "followed_id" to the field. Therefore, we don't need to assign value to the field like using f.hidden_field :followed_id, value: #user.id.
And the reason why we have to use a field to pass followed_id to params is because HTTP server is stateless, it doesn't remember you are creating a relationship with which user.
One of the advantages of writing form_for current_user.relationships.build(followed_id: #user.id) instead of standard form_for #relationship is we don't need to write "if-condition" in controller like this:
unless current_user.nil?
if current_user.following?(#user)
#relationship=current_user.relationships.find_by(followed_id: #user.id)
else
#relationship=current_user.relationships.new
end
end
params will be sent to the controller which belongs to the instance's model. "post" method will go to action create, "delete" will go to destroy, "patch" will go to update, etc.
params will be a hash with another hash inside like { instace_name: { field_1: value1, field_2:value2 } } or full params as below
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"afl+6u3J/2meoHtve69q+tD9gPc3/QUsHCqPh85Z4WU=",
"person"=>{"first_name"=>"William", "last_name"=>"Smith"},
"commit"=>"Create"}

How to hide parts of the view given a user role on Rails 4

I'm trying to hide parts of my views depending on the User role.
So let's say I want only admins to be able to destroy Products. Besides the code in the controller for preventing regular users from destroying records, I would do the following in the view:
<% if current_user.admin? %>
<%= link_to 'Delete', product, method: :delete %>
<% end %>
The previous code works, but it's prone to errors of omission, which may cause regular users to see links to actions they are not allowed to execute.
Also, if I decide later on that a new role (e.g. "moderator") can delete Products, I would have to find the views that display a delete link and add the logic allowing moderators to see it.
And if there are many models that can be deleted only by admin users (e.g. Promotion, User) maitenance of all the ifs would be pretty challenging.
Is there a better way of doing it? Maybe using helpers, or something similar? I'm looking for something maybe like this:
<%= destroy_link 'Delete', product %> # Only admins can see it
<%= edit_link 'Edit', promotion %> # Again, only admins see this link
<%= show_link 'Show', comment %> # Everyone sees this one
I found these two questions that are similar to mine, but none of them answered my question:
Show and hide based on user role in rails
Ruby on Rails (3) hiding parts of the view
I strongly recommend pundit.
It allows you to create "policies" for each model. For your Product model you might have a ProductPolicy that looks something like this
class ProductPolicy < ApplicationPolicy
def delete?
user.admin?
end
end
In your view you can do something like this
<% if policy(#post).delete? %>
<%= link_to 'Delete', product, method: :delete %>
<% end %>
If later on you want to add a moderator role, just modify the policy method
class ProductPolicy < ApplicationPolicy
def delete?
user.admin? || user.moderator?
end
end
So I kind of figured a way to move the IFs out of the view. First, I override the link_to helper in my application_helper.rb:
def link_to(text, path, options={})
super(text, path, options) unless options[:admin] and !current_user.admin?
end
Then on my views I use it as:
<%= link_to 'Edit Product', product, admin: true, ... %>
This prevents regular users from seeing admin links, but for other html tags with content inside, such as divs, tables etc., an if would still be needed.
CanCan is another gem that lets you define "Abilities" per user role.
In views you can use something like if can? :delete, #post to check if the
user may delete that specific post.
Using the CanCan and Role gems, what is still needed is a way to Check The Route and see if "current_user" has permissions to access that Route based on their role(s) - then show/hide based on that.
This saves the user clicking on things and getting told they cannot see it - or us having to write per-item "if" logic specifying what roles can see what list-items (which the customer will change periodically, as roles are changed/refined) around every single link in one's menu (consider a bootstrap menu with 50+ items nested in groups with html formatting, etc), which is insane.
If we must put if-logic around each menu-item, let's use the exact same logic for every item by checking the role/permissions we already defined in the Ability file.
But in our menu-list, we have route-helpers - not "controller/method" info, so how to test the user's ability to hit the controller-action specified for the "path" in each link?
To get the controller and method (action) of a path (my examples use the 'users_path' route-helper) ...
Rails.application.routes.recognize_path(app.users_path)
=> {:controller=>"users", :action=>"index"}
Get just the controller-name
Rails.application.routes.recognize_path(app.users_path)[:controller]
=> "users"
Ability uses the Model for its breakdown, so convert from controller name to it's model (assuming default naming used) ...
Rails.application.routes.recognize_path(app.users_path)[:controller].classify
=> "User"
Get just the action-name
Rails.application.routes.recognize_path(app.users_path)[:action]
=> "index"
And since the "can?" method needs a Symbol for the action, and Constant for the model, for each menu-item we get this:
path_hash = Rails.application.routes.recognize_path(app.users_path)
model = path_hash[:controller].classify.constantize
action = path_hash[:action].to_sym
Then use our existing Abilty system to check if the current_user can access it, we have to pass the action as a symbol and the Model as a constant, so ...
<% if can? action model %>
<%= link_to "Users List", users_path %>
<% end %>
Now we can change who can see this resource and link from the Ability file, without ever messing with the menu, again. But to make this a bit cleaner, I extracted out the lookup for each menu-item with this in the app-controller:
def get_path_parts(path)
path_hash = Rails.application.routes.recognize_path(path)
model_name = path_hash[:controller].classify.constantize
action_name = path_hash[:action].to_sym
return [model_name, action_name]
end
helper_method :get_path_parts
... so I could do this in the view (I took out all the html-formatting from the links for simplicity, here):
<% path_parts = get_path_parts(users_path); if can?(path_parts[1], path_parts[0]) %>
<%= link_to "Users Listing", users_path %>
<% end %>
... and to make this not take all day typing these per-menu-item if-wraps, I used regex find/replace with capture and wildcards to wrap this around every list-item in the menu-item listing in one pass.
It's far from ideal, and I could do a lot more to make it much better, but I don't have spare-time to write the rest of this missing-piece of the Role/CanCan system. I hope this part helps someone out.

Use button_to to create habtm join?

G'day all.
In a Rails app I have 2 models: users and spots, with a habtm relationship and join table. In the spot/show action I can create a form to ask the current user if they have visited this current spot (checkbox) and click save to create a record in the join table.
This works well (so I know my models and relationships are all good) however is not that elegant. Is there a way to do this without having to use a checkbox and submit button? Preferably with just a button?
My research suggests the rails button_to might do it, but I can't find a working example.
Many thanks.
Yes, button_to will work fine:
<%= button_to "I've visited here", {:action => "visited", :id => #spot} %>
Will generate a button that when pressed will pass in the #spot in the params as expected. You can then (assuming you have a current_user method because you're using a standard user model framework), do something like this:
def visited
spot = Spot.find(params[:id])
current_user.spots << spot
redirect_to :action => "show", :id => spot
end
Hope that helps.

Resources