I am new to rails (and non ASP.NET web development). For creating a user signup page with a radio button, I'm doing the following. However the user.roles is not updated. What could be the problem?
(I edited the user model created by sorcery gem to add a new string field roles.)
view/users/new.html.erb
<%= form_for #user do |f| %>
..
<div class="field">
<%= radio_button_tag(:assistanttype, "TA") %>
<%= label_tag("TA") %>
<%= radio_button_tag(:assistanttype, "RA") %>
<%= label_tag("RA") %>
</div>
Try changing the code to:
<%= form_for #user do |f| %>
..
<div class="field">
<% %w{TA RA}.each do |value|-%>
<% f.radio_button :assistanttype, value %> <% value %><br />
<% end -%>
</div>
%w{} is used to put string inside and automatically change it to an array.
Example:
%w{A B C} = [A, B, C]
Related
I am working on a new app like SNS. And I stack the case users update the group member with Cocoon. UserGroup model includes current user but current user should not be edited, so I don't want to display current user when the user edit the group member.
I want to skip the column includes current user.
apps/views/group/edit.html.erb
<div class="formItem">
<h3>Group</h3>
<%= f.fields_for :user_products do |m| %>
<div id="links">
<%= render 'maker', f: m %>
</div>
<% end %>
<%= link_to_add_association "Add member", f, :user_products, partial: 'maker' %>
</div>
apps/views/groups/_member.html.erb
<div class="nested-fields">
<div class="formField">
<%= f.collection_select :user_id, #cofounder, :id, :username, {}, class: "founderSelect" %>
<%= link_to_remove_association "Remove member", f %>
</div>
</div>
If I understand it correctly you want to remove current_user from the cofounder selection group right?
You could do it with #cofounder.where.not(id: current_user.id)
<%= f.fields_for :user_products, f.object.user_products.where.not(user_id: current_user) do |m| %>
<div id="links">
<%= render 'maker', f: m %>
</div>
<% end %>
I got it.
I have two controller project_controller.rb and service_controller.rb . I have a field in Project named as service_id.
When I create project, the value of service_id is kept as null by default. So now I need to update the service_id field of Project with the id of Service.
For That I have created a form using the project object. shown below :
<%= form_for(#project) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<% #services.each do |service| %>
<%= f.label "#{service.name}" %> # Here I want to pass the array of
<%= f.check_box :service_id %> # service_id through value of check box
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Now in source it is coming like this..
Please help .. Thanks
If you refer to Rails API you see
check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
so, to pass custom value to check box, you need
<%= f.check_box :service_id, {}, service.id, '' %>
i have this form:
<%= form_for(#candy) do |f| %>
<div class="">
<%= current_user.totalcandy %>
</div>
<div class="">
<%= f.label :candy, 'add a candy' %><br />
<%= f.number_field :candy%>
</div>
<div class="">
<%= f.submit('ADD') %>
</div>
<% end %>
how do i add that input value into totalcandy and save it? Thanks
You're getting confused with how Rails works
Rails is server-side
You're trying to calculate client-side
You have to process them separately
Modularity
You'll have to calculate this on the back-end (once you've submitted the form)
Rails views are there to show an HTML page, and won't change unless a request is performed to the server
In keeping with Rail's modular structure, you need to keep your logic in your controller, your inputs in your views, and your data manipulation in your models. Therefore, you only need to pass the different Candy's to your controller, allowing it to sum the total:
#app/views/candys/new.html.erb
<%= form_for(#candy) do |f| %>
<%= f.label :candy, 'Add a candy' %>
<%= f.number_field :candy%>
<%= f.submit %>
<% end %>
#app/controllers/candys_controller.rb
def new
#candy = Candy.new
end
def create
#candy = Candy.new(candy_params)
#candy.save
#total_candy = #your logic here
end
private
def candy_params
params.require(:candy).permit(:candy)
end
I am newbie in rails and I want to filter my page content using muptiple tags. I am using act_as_taggable_on gem and I managed to have a tag-cloud and filter my content according tags. I used the following tutorial (http://railscasts.com/episodes/382-tagging).
Now I couldn't manage to filter using multiple tag_types.
I added in my model/ article.rb the following code
acts_as_taggable
acts_as_taggable_on :assetType, :productType
in controller I don't know to write multiple tags . I tried the following way
def index
if (params[:assetType] and params[:productType])
#articles = Article.tagged_with(params[:assetType]).tagged_with(params[:productType])
else
#articles = Article.all
end
end
In my view in index.html.erb I have
<div id="tag_cloud">
<% tag_cloud Article.productType_counts, %w[s m l] do |tag, css_class| %>
<%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>
</div>
<div id="tag_cloud_asset">
<% tag_cloud Article.assetType_counts, %w[s m l] do |tag, css_class| %>
<%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>
</div>
<div class="article-content">
<% #articles.each do |article| %>
<h3><%= article.title %></h3>
<p><%= article.content %></p>
<% end %>
and in my _form I have
<%= form_for(#article) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :assetType_list, "Tags (Asset Type separated by commas)" %><br />
<%= f.text_field :assetType_list %>
<%= f.label :productType_list, "Tags (Product Type separated by commas)" %><br />
<%= f.text_field :productType_list %>
</div>
<div class="actions">
<%= f.submit %>
</div>
Could someone help me how should I modilfy my controller, index and _form page? now it is showing all my posts and when I click on tags the content is not changed
Using this as a basic reference point:
https://github.com/mbleigh/acts-as-taggable-on#finding-tagged-objects
Try this:
def index
tags = []
tags << params[:assetType] unless params[:assetType].blank?
tags << params[:productType] unless params[:productType].blank?
if tags.count == 2
#articles = Article.tagged_with(tags)
else
#articles = Article.all
end
end
Adjustments:
Checking each parameter for null and empty string using blank check. Perhaps it's possible that null and blank are the same in this context.
Adding tags to an array, so that I can pass them all at once. Not just to simplify the call, but you can have more explicit control over the matching style by adding additional parameters to the call (such as match all or any tags).
Hope that helps, good luck!
I'm using the AASM gem to manage states on one of my models. Right now, I'm using a form_for in a javascript popup to change the state, but it's not working:
<h2>Set the state:</h2>
<%= form_for(#tracker) do |f| %>
<% if #tracker.errors.any? %>
<div id="error_explanation">
<h2>Uh-oh. We've got some problems</h2>
<% #tracker.errors.full_messages.each do |msg| %>
<%= msg %><br />
<% end %>
</div>
<% end %>
This tracker is currently: <%= #tracker.state %><br />
<%= select_tag :state, options_for_select(Tracker::STATEDESCRIPTIONS.map { |event| [event.to_s.humanize, event]}) %>
<%= f.submit %>
<% end %>
What I'd really like to do, though, is contain the form all in a single button, but I'm not sure what to use for that? button_to?
You should use f.select instead of select_tag. That way, the resulting select HTML tag will be associated with the form_for(#tracker), and the chosen state will be correctly mapped to #tracker in the controller action in question.