I am trying to learn by doing and need some help.
So formtastic can handle belongs_to associations (like Post belongs_to :author), rendering a select or set of radio inputs with choices from the parent model.
What I am trying to do is create a post without having to select the author in the create form.
I want to set the author id via a hidden form field but when I try this
<%= f.input :author, :as => :hidden, :value => '33' %>
I get uninitialized constant author and tried various other ways that Ive failed to get to work.
The closest Ive got has been by trying to customise the choices for the select and hiding the select as follows:
<%= f.input :author, :as => :select, :collection => ["33"], :input_html => { :style => 'visibility: hidden' } %>
But even though the above hides the select and works, it still displays a form label for author which I dont want it to display
Here is an example to better explain my scenario if what Im trying to achieve is still unclear:
Imagine you have a list of authors page and as an author you click on your name to get into an area where you can manage various things and create posts.
The problem: When you click create a post and the form renders, you dont want to have to select your name from one of the fields to tell the app that the post you are creating belongs to you because you want the system to know its yours because you are in a config area that belongs to you.
I am trying to do this by using a hidden form field to set the author id so that the post is saved with the correct association when the user submits the form.
Can anyone provide me with an example of how to do this? and as Im a learner maybe there is a better way to approach this so can anyone please advise and point me to some good examples
You should not be populating it in a hidden form field; hidden form fields can still be changed by the user and in the end you are allowing users to create posts by other authors.
The correct way to do this would be to assign #post.author = current_user in the create action of your PostsController.
Related
I am not very familiar with ActiveAdmin or rails in general, so sorry if I use some incorrect phrasing.
I have a model for an Athlete, which has an attribute "notes". This is described in the permit_params of the athlete.rb.
On an additional page, I have the following:
f.input :athlete, :collection => Athlete.all.sort_by(&:last_name), :required => true
I would like to find a way to, if the :notes is not empty, display it.
If I could display it as a "row" on the form, that would be great.
Thanks!
Hoping this will be a straight forward question, but is anyone able to let me know the best way of populating a hidden field based on a value on another table.
I currently have 2 tables - table_numbers which has the following fields - id and value(decimal), and table_records which has an amount field.
On the form to add a new record I have the following to add a value to it
= f.association :table_number, :collection => table_numbers.order('value ASC'), :label_method => :value, :prompt => "Select a value", :label => "value"
At the moment this is populating the number_id on the records table, but displaying the value on the form when adding a record. What I would like is to get the value as well to be able to run a calculation on the value and amount.
What would be the best way to do this? Update the line above or do I need to add extra code?
Thanks
You can perfom calucaltions in before_save callback in model. If you want to display calculations, use helper and show it.
So I have a Company, Subcompany models, and I use the Brand model as the masterlist for all company and subcompanies. Right now, when the admin user creates a new company, they have to use the dropdown list of brands to put a new company or subcompany into the list.
This becomes unscalable as there are 10000's of companies. The easiest way to filter out companies is to filter by category. so assuming all brands have a category attribute, I was wondering if there is any way, within the ActiveAdmin framework, to allow the admin user to filter the dropdown list by a category?
Here is what I have so far, it allows me to do a drop down on all the brands. But I want a way for the user to dynamically shrink the list by picking a category.
form do |f|
f.inputs do
f.input :name, :as => :select, :collection => Brand.all.collect {|brand| brand.name }
f.has_many :sub_companies, allow_destroy: true do |sub|
sub.input :name, :as => :select, :collection => Brand.all.collect {|brand| brand.name}
end
end
actions
end
There is no build in way, you can do one of the following thinks:
User select2, a suggest/search supporting select field, you can find some integration help here
You can write a javascript to fill a second select by the value of the first one.
Still fairly new to RoR, let alone Ruby itself.
Here's my issue. I'm using Rails 4 and Ruby 2.1.0
I have a User class with this relationship:
has_many :roles, ->{ uniq }, :through => :user_roles
accepts_nested_attributes_for :roles
Now, the table 'roles' simply has a list of roles, such as 'admin', 'moderator', 'visitor', 'artist', etc.
The table 'user_roles', is just a relational table, with the role's key and the user's key.
So, to clarify, 'users' has a one to many relationship with 'user_roles' and "user_roles' table has a many to one relationship with the 'roles' table.
Something like this:
users: id, name, email #ie: 12, 'mikey', 'mike#foobar.com'
user_roles: id, user_id, role_id #ie: 4324, 12, 8
roles: id, name, weight #ie: 8, 'moderator', 11
I've been given the task of adding a checkbox toggle in the admin section so that an admin can grant the moderator role to any one user.
Each admin page is for one specific user. Therefore, the form_for is based on the #user instance, depending on which user the admin selected for editing.
RailsCasts #196 kind of helped me understand the concept, but it's platform is just different enough that I'm not sure how to exactly translate it into what I need. Likewise, there are some similar questions here on StackOverflow as well - but again, I can't seem to get it to translate correctly into my situation.
What I do have, I think should be at least somewhere in the realm of the right direction. THen again, maybe I'm way off. Here is my form code:
= form_for #user, :url => admin_user_path(#user), html:{class: 'form_horizontal'} do |f|
%br
%h4{class: :dc_green}Logistical Information
= f.label :display_name, "Display Name"
= f.text_field :display_name
= f.label :email, "eMail"
= f.text_field :email
%br
= f.fields_for :roles do |a|
= a.check_box :moderator, { inline_label: :none }
= a.label :moderator, {for: :moderator, class: "checkbox inline"}
%br
The error I'm getting with this form code is: undefined method `moderator'
Thank you in advance for anyone who can offer support :)
I guess I should probably add that I'm also clueless as to how I should handle this in the controller once it is submitted. But, first things first.
UPDATE
So, the reason I'm getting the undefined error is because it doesn't exist for that user. Which is bad. I need to be able to add the role to the user or remove the role. However, the only roles coming through are the roles the user has already been assigned to. So I'm totally doing this wrong. Now I'm really in the dark.
I have three models:
class Client < ActiveRecord::Base
has_many :balancesheets
has_many :investment_assets, :through => :balancesheets
class Balancesheet < ActiveRecord::Base
belongs_to :client
has_many :investment_assets, :dependent => :destroy
accepts_nested_attributes_for :investment_assets, :allow_destroy => true
class InvestmentAsset < ActiveRecord::Base
belongs_to :balancesheet
belongs_to :client
I have two questions that have to do with the foreign key client_id. First, when I create a new balancesheet, I use collection_select to select the client from the list. I would rather put the new balancesheet link on the client show page and just pass the client id through to the form so I don't have to choose a client from a list or type it in a field. So first, how do I do that?
Second, the investment_asset model is nested in the balancesheet form. Everything works just fine, except the client_id attribute for the investment_asset is blank. Im not sure why because my associations seem to be okay. So the question is, how do I pass this client_id attribute down through the nested form? I can post models or controllers, just let me know.
UPDATE I have since found the answer to my first question here. However, I am still trying to figure out the second part based on that answer, which is, how do I pass this client_id down through a nested form? To show how it worked passing user id when creating a balancesheet, here is the client show page link:
<%= link_to 'New BalanceSheet',new_balancesheet_path(:id => #client.id) %>
In the balancesheet form I have a hidden field:
<%= f.hidden_field :client_id %>
And in the Balancesheet Controller this is the new action:
#balancesheet = Balancesheet.new(:client_id => params[:id])
This works just fine. So for an investment_asset I am using Ryan Bates nested_form gem which has a little bit different syntax for a link. So inside the balancesheet form the link to add a new investment_asset is:
<%= f.link_to_add "Add asset", :investment_assets %>
I can't figure out how to pass the user id like I did on the balancesheet with:
(:id => #client.id)
I can put the same thing in a new action in the investment_asset controller and add a hidden field, but I'm not sure how to pass it in on the link. Thoughts?
This might be a total hack for all I know, but all I know is very limited. What I ended up doing here was user jQuery to take the id from the balancesheet and force it into a hidden field for investment_asset. jQuery:
// get the value from the balancesheet hidden field that contains the user_id value
var balsheetclientid = jQuery('#hiddenid').attr('value');
// insert it into the value attribute in the hidden field with class name .iaclientid
jQuery('.iaclientid').val(balsheetclientid)
Then my hidden field looks like this:
<%= f.hidden_field :client_id, :class => 'iaclientid', :value => '' %>
It works just fine. Hopefully that's a valid way of doing it.