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
Related
I'm building a rails app that has a an object that is created /nested underneath two objects.
Routes.rb
resources :pages do
resources :referralpages do
resources :rewards do
end
end
end
I've just added the rewards resource and have this for my form for creating a new reward
<%= form_for ([#referralpage, #reward]) do |f| %>
<% if #reward.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#reward.errors.count, "error") %> prohibited this reward from being saved:</h2>
<ul>
<% #reward.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :level %><br>
<%= f.text_field :level, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :dscount %><br>
<%= f.text_field :discount, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
I need help getting the form_for ([#referralpage, #reward]) portion working.
Here's the error message I'm getting when clicking the new reward button:
undefined method `referralpage_rewards_path'
<%= form_for ([#referralpage, #reward]) do |f| %>
my guess is that it's routing to the incorrect path. What's the proper syntax to get this to work?
I think it should render this path
new_page_referralpage_reward
The point of this feature is to render rewards in the referral/show.html.erb page
I have created an index partial in my rewards view file and am rendering it in the show action of the referralpages/show.html.erb file.
I think you cannot put more than one resource path in form_for which cause invalid path.
Why you want to put 2 resource path in form?
Do you plan to save the same data for referral & rewards Model?
If, yes use just one path and make a create method in your controller to save to other model.
From this point of view:
The point of this feature is to render rewards in the
referral/show.html.erb page
If you only plan to render the data of rewards to referral/show.html.erb,
in your referral controller
def show
#rewards = Reward.all #example
end
Unless, you have model relationships like:
#Reward Model
belongs_to :refferal
#Referral Model
has_many :rewards or has_one :reward
With model realtionship:
def show
#referal = Referral.all #example
end
show.html.erb View # iterate it:
<%for referral in #referral%>
<% referral.rewards %> # need to iterate if has many
or
<%= referral.reward... %>
<%end%>
I'm creating a Rails app that will take in input from two models and their respective forms, and then log into a website with Mechanize and do hundreds of tasks there.
The first form consists of the user's login information (the username and password, using the model User), and the second one is a long list of term names and their respective definitions (using the model Term and uploaded into the form as an Excel doc).
I have no need to associate these two models (unlike in similar SO questions, which seem to deal with nested models). Once this Mechanize task completes, I will destroy both models' objects from the database.
The two pieces of the app (logging in to the website; and uploading the terms and using them to interact with the website) both work perfectly in separate scripts.
My question is: How can I take in both models' information on one webpage and coordinate the controller(s) accordingly? In this situation, can I create both objects in one controller? (If not, that's fine, as long as there's an alternative; I'm game for whatever will get this to work.)
I'm posting some of my code below. I'm happy to answer any questions you may have. Please keep in mind I am pretty new at Rails, so is not very elegant code:
The User model:
class User < ActiveRecord::Base
attr_accessor :username, :password
end
And the Term model:
class Term < ActiveRecord::Base
attr_accessible :name, :definition
end
In my terms controller (the forms are located in the index action and the script is run in the show action):
def create
#term = Term.new(params[:term])
#user = User.new(params[:user])
if (#term.save && #user.save)
flash[:notice] = "Your terms have been sent for processing."
redirect_to terms_path
else
render :action => 'index'
end
end
def show
#term = Term.all
agent = Mechanize.new
page = agent.get('www.myurl.com')
#log into the website
#user.username = myform.field_with(:id => "userfield").value
#user.password = myform.field_with(:id => "passfield").value
#and then enter the term information into the website's forms
rest of Mechanize code goes here...
end
And in views/terms/index.html.erb:
#the login form:
<%= render 'loginform' %>
#the term file uploader:
<%= form_tag import_terms_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
<br>
<table id="terms">
<tr>
<th>Name</th>
<th>Definition</th>
</tr>
#displays the uploaded terms on the index page
<% #terms.each do |term| %>
<tr>
<td><%= term.name %></td>
<td><%= term.definition %></td>
</tr>
<% end %>
</table>
<p><%= link_to 'Update website with these terms', terms_update_terms_path %></p>
And in views/terms/_loginform.erb:
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label_tag(:username, 'Username') %><br />
<%= f.text_field_tag(:user, :username) %>
</div>
<div class="field">
<%= f.label_tag(:password, 'Password') %><br />
<%= f.password_field_tag(:user, :password) %>
</div>
<% end %>
And in views/terms/_termform.html.erb
<%= form_for(#term) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :definition %><br />
<%= f.text_area :definition %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
If you just need to save the data and truly don't want to associate the models, I would save the Term separately:
<%= form_for #user do |f| %>
<div class="field">
<%= f.label :username, 'Username' %>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= label_tag :name %>
<%= text_field_tag :name %>
</div>
<div class="field">
<%= label_tag :definition %>
<%= text_field_tag :definition %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Then in your controller:
#term = Term.new
#term.attributes = {
:name => params[:name],
:definition => params[:definition]
}
#term.save
If you are adding multiple term fields to the form, I would consider something like Cocoon, and just add an each loop for saving the Terms in the controller.
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]
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.
I have a form in my rails app that creates an item in my database, but when I submit the form, it creates 3 items per click.
I have 2 other forms that add things to the same database, but they are on different pages, could that be related?
This is my form on the "new debate" page:
<%= form_for(#debate) do |f| %>
<div class="field">
<%= f.label :proposition %><br />
<%= f.text_field :proposition %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I also have another form on the "show debate" page that appears twice:
<%= form_for(#debate.debates.create) do |support_form| %>
<div>
<%= support_form.label :content %><br />
<%= support_form.text_area :content %>
</div>
<%= support_form.hidden_field :is_supporting, :value => is_supporting %>
<div class="actions">
<%= support_form.submit %>
</div>
<% end %>
And when I click on the submit button on any of the 3 forms, I get 3 new debates.
I think your code, might be creating those extra records.
= form_for(#debate.debates.create) do |support_form|
If my assumption is correct .debates is an association, and you are creating that association with that line.
Try using build
= form_for(#debate.debates.build) do |support_form|