Rails - Is there a way to update a single attribute? - ruby-on-rails

I was just wondering, I have a model that, besides the id's from other models (FK), it has a single attribute boolean. I want to know how can I create a button that changes this boolean and just that
My model in question is this one:
class Squad
belongs_to :player
belongs_to :team
end
I want to create a button on the team#show page so the player that owns this team can change the boolean of squad. How can I do this and how would look like my controllers?
Thanks :)!
-Edit-
I'm using a link like this:
<%=link_to("Change status", squad_path(sqd, :status => true), :method => :put, :confirm => "Sure?")%>
Where sqd is part of my query. Is this link wrong?

<%= link_to("Change status", squad_path(sqd, "squad[status]" => true), :method => :put, :confirm => "Sure?") %>
in your controller (it is pretty common)
def update
#squad = Squad.find params[:id]
if #squad.update_attributes params[:squad]
...
end
end

Yes there is. The method is called "update_attribute". It takes two arguments, the name of the field and the value.
squad.update_attribute(:boolean_field,true) # or false
Based on updated question
def update
#squad = Squad.find(params[:id])
if #squad.update_attribute(:status,params[:status])
...
end
end

What's the name of your attributes ?
Since it belongs to player, you can access it with player.squad.name_of_your_attributes = new_value. Don't forget to save your object if you want the changes to be saved in your DB.
Also, you could read that
EDIT: fl00r answered your edited question, no need that i repeat what he wrote.

Related

Delete from table if checkbox not checked

In my Rails app I have Users, Roles, and Permissions.
When creating/editing a Role, you can choose which permissions are enabled by checking their checkbox which will save the Permission in a table called 'roles_permissions' (basically on permissions that are allowed are stored in the join table).
So my edit role method is as follows:
def edit
#role = Role.find(params[:id])
#permissions_by_controller = Permission.order('controller asc').group_by(&:controller)
end
and the update method (the patch):
def update
#role = Role.find(params[:id])
if #role.update_attributes(role_params)
redirect_to roles_path, :notice => 'Article updated!'
else
render 'edit'
end
end
and the params:
def role_params
params.require(:role).permit(:name, permission_ids: [])
end
In the edit view I have a checkbox like so:
<%= check_box_tag "role[permission_ids][]", permission.id, #role.permissions.include?(permission), :id => permission.id, :class => 'switch__checkbox' %>
This works fine for when I check the checkbox and it saves.
However when I uncheck the checkbox and save, it doesn't remove the permission... presumably becuase no param is passed back. How do I solve this?
The associations are set as:
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => 'users_roles'
has_and_belongs_to_many :permissions, :join_table => 'roles_permissions'
end
class Permission < ActiveRecord::Base
has_and_belongs_to_many :roles, :join_table => 'roles_permissions'
end
Note: I've also noticed that Roles cannot be removed from a User in the same way as it uses checkboxes.
A classic problem. f.check_box solves this for you, by returning false when unchecked, but check_box_tag does not.
You can solve it with a hidden field with an empty value just before the tag.
<%= hidden_field_tag "role[permission_ids][]", '' %>
<%= check_box_tag "role[permission_ids][]", permission.id...
The form knows to populate the empty value field when nothing is selected. (If it's clearer for you, you can also put 'false' instead of an empty string.)
Complementing steel's answer (and since I can't comment on his answer bc of my rep)
I'll say to anyone looking for a different way to solve this.
I prefer attaching the hidden_field to the form .
<%= form.hidden_field :permission_ids, { multiple: true, value: '' } %>
Since we are calling form.hidden_field, the name and id will already contain role and it will add permissions_ids as a key inside role.
Multiple will add the needed square brackets.
The resulting html tag will be:
<input multiple="multiple" value="" type="hidden" name="role[permission_ids][]" id="role_permission_ids">

Rails 3, change or fill submitted form values in controller

I have two independent "component" models, contacts and articles. Contacts come in many types (producers, exporters etc...).
In the new article form I have a dropdown selector with contacts (id and title) and want to store the selected value and text in the article table.
In the new article form view:
<%= f.select :producer_id, options_for_select(producers, #article.producer_id) %>
That works and producer_id is stored in article table.
That's clear and logical to me, but in some cases I also need to store the selected contact's title in producer_title.
I have read many different options like "do it in model, before save", or "do it in controller", and I have done it inside controller.
Article controller (only part from update):
#cont_name is producer title from Contacts
def update
params[:article][:producer_title] = Contact.where(id: params[:article][:producer_id]).pluck(:cont_name).first
end
This works, but is it the best-practices approach to this problem?
Also, why I can't get it to work if I change the params[producer_id] part to use: id: params[:producer_id] ?
Best regards and thanks.
How about something like the following instead:
def edit
#article = Article.find(params[:id])
#producers = Contact.where(type: "producer") # or however you distinguish between producers and other contacts
end
Then in your form change it to:
f.select :producer_id, options_from_collection_for_select(#producers, "cont_name")
# you might need to do (#producers, "cont_name", "cont_name"), can't quite remember
Then your update action will be much simpler:
def update
#article = Article.find(params[:id])
if #article.update_attributes(params[:article])
...
else
...
end
end
The reason :id => params[:producer_id] doesn't work is that params is a nested hash, so something like:
params = { :article => { :producer_id => 34, :title => "Cool Article" }, :another_key => { :key => :value } }
So to access the producer_id you first have to retrieve the article hash, otherwise it will only look through the first set of keys which include article and another_key in the example above, but don't include producer_id.

Rails nested form with accepts_nested_attributes_for with an unfortunate model name

I have a Parent model named "Controller" (Mature app, and not my decision)
belongs_to :controller
accepts_nested_attributes_for :controller
Form:
= f.fields_for :controller do |c|
= c.hidden_field :id, :value => #controller.id
= c.text_field :slw_type
which doesn't get displayed.
= f.fields_for :literally_anything_else do |c|
= c.hidden_field :id, :value => #controller.id
= c.text_field :slw_type
if change the variable name to anything else, the form builds. I have a hunch that it's a rails specific reserved name.
Question:
What's the problem? and how can I make this work?
SOLVED:
The issue was that the parent model wasn't associated with the child model yet. My mistake for not providing all the information necessary.
This worked.
def new
#controller = Controller.find(params[:controller_id])
#inspection = Inspection.new(:controller => #controller)
Therefore my fields_for form builder also worked.
Pick some innocuous variable name. not_really_a_controller or whatever. Use that for your variable and your form. Then, in your actual controller (e.g. ActionController::Base descendent), rename the incoming param so the model doesn't know any different, like so:
before_filter :filter_params
private
def filter_params
if params[:not_really_a_controller]
params[:controller] = params.delete(:not_really_a_controller)
end
end
I've used this strategy for similar reasons in the past, though not specifically for controller. Worth a try though!

Passing IDs through a new Form

This question is about a different approach I'm trying to the one asked here:
Passing IDs through a new Form
I have a group#view page, that is accessed by a Person. In this page, the Person can see the members of the group via methods I developed. The problem is that I need to create the model Honors using the Id from the group, the id from the person accessing the page, and the id from a member of this group.
In my Honors controller I have:
def create
#person = Person.find(current_person)
#asked_groupmembership = #person.owned_group_memberships.find_all_by_status(true,:include => [:group, :member])
#asked_groupmembership.each do |agm|
#honor = Honor.create(:group => Group.find(params[:group_id]),
:person => Person.find(current_person), :honored => Person.find(agm.member.id))
end
if #honor.save
...
end
In my view I have a link that directs the person to the form in order to create a new honor:
<% #asked_groupmembership.each do |agm| %>
<%= link_to "Create Honor", new_honor_path(:group_id => #group.id, :person => current_person.id,
:honored => agm.member.id) %>
But in my forms I can't get the ids and stuff
<% form_for(:honor, :url => honors_path(:group_id, :person,
:honored)) do |f| %>
The error I get is that I can't find Group without an Id.
Any ideas? Thanks.
##Edited2##
Changed my crontroller
def new
##person = Person.find(params[:person])
##honored = Person.find(params[:honored])
##group = Group.find(params[:group_id])
#honor = Honor.new
end
def create
#person = Person.find(current_person)
#honor = Honor.create(:group => Group.find(params[:group_id]),
:person => Person.find(params[:person]),
:honored => Person.find(params[:honored]))
if #honor.save
...
end
First, it seems like you are missing a controller method. Along with every form that creates a new object there are typically two controller methods
new
Gathers up any data that the form needs to render itself
Renders the form
create
Collects the data from the form
Creates the new object
It looks to me like you are missing the new method. In the new method you would gather up all the hidden data that the form needs (e.g. the information that the user is not going to type in directly, like the #person info). Then, I would put this information in your form using hidden form parameters (rather then trying to put it in the form URL).
Objective: From the group#view, loop through and create and "Add Honor" link for each member of the group, while keeping track of the specific member being honored, group in which said member is honored, and the person who is honoring.
The following accomplishes that objective.
Route:
match "honors/:group/:person/:honored" => "honors#create", :as=>"my_custom_new_honor"
Group view:
<% #asked_groupmembership.each do |agm| %>
<%= link_to "Create Honor", my_custom_new_honor_path(:group=> #group.id, :person => current_person.id,:honored => agm.member.id) %>
Honor Controller, Create Method
#honor = Honor.create(:group => Group.find(params[:group_id]),
:person => Person.find(params[:person]),
:honored => Person.find(params[:honored]))
In this scenario you do not need the honor#new method, you're going directly to the create method. This assumes all the relationships are established correctly.

Ruby/Rails: dynamically change attribute in shared partial

This should be a layup for someone...
I'm trying to change a form field's attribute depending on which controller/model is calling the partial containing the form fields...
The issue (below) is with parent_id... which references one of two columns in a dogs table. It needs to either be kennel_id or master_id depending on which view this partial is being rendered in.
Not comfortable enough, yet, with Ruby/Rails language/syntax/tools to dynamically change this without getting bogged down in if/else statements.
I'm calling a shared partial and passing in a local variable:
= render "dogs/form", :parent => #kennel
or
= render "dogs/form", :parent => #master
In the partial I'd like to:
= form_for ([parent, target.dogs.build]) do |f|
= render "shared/error_messages", :target => parent
.field
= f.label :name
= f.text_field :name
.field
= f.hidden_field :parent_id ### <= PROBLEM
.actions
= f.submit 'Save'
Just thinking out loud:
I don't know if the parent-models have the proper names for it, but you could do something like:
= f.hidden_field "#{parent.class.name.underscore}_id"
But that doesn't look right. So, why not pass it as an argument?
= render "dogs/form", :parent => #master, :foreign_key => :master_id
Or, create aliases on the dog model to handle some sort of dynamic delegation:
class Dog
def parent_id=(parent_id)
case parent.class
when Master then self.master_id = parent_id
when Kennel then self.kennel_id = parent_id
end
end
def parent_id
case parent.class
when Master then self.master_id
when Kennel then self.kennel_id
end
end
end
But that sucks too. Could the relation be polymorphic? Then you can leave out the switching.
class Dog
belongs_to :owner, :polymorphic => true
end
= f.hidden_field :owner_id
Just some ideas. Hopefully one of them makes sense to you...
Wow, my initial answer wasn't even close. I think what you'll want is a polymorphic association: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations This way, the parent can be whatever class you need it to be.

Resources