I have the Following
Page Model
Client Model
Business Model
There is No Relationship between any of these Models.
I would like to Have on the Show Template of the Page to have the Form_for's
One for the Client
One for the Business
Is This Possible?
Current Have The Following:
<div id="sidebar">
<%= form_for (#business) do |f| %>
<div id="contact_form_name">
<p>Company</p>
<%= f.text_field :company_name, :class =>'form_input_small' %>
<%= f.submit 'Submit', :class => 'button' %>
<% end %>
</div>
<div id="sidebar">
<%= form_for (#client) do |f| %>
<div id="contact_form_name">
<p>First Name</p>
<%= f.text_field :first_name, :class =>'form_input_small' %>
<%= f.submit 'Submit', :class => 'button' %>
<% end %>
</div>
Error I am Getting in the Log is the following
<div id="sidebar">
78: <%= form_for (#business) do |f| %>
79: <div id="contact_form_name">
80: <p>Company</p>
81: <%= f.text_field :company_name, :class =>'form_input_small' %>
app/views/pages/show.html.erb:78:in `_app_views_pages_show_html_erb___1556847543_65073939124600'
app/controllers/pages_controller.rb:9:in `show'
Routes
businesses GET /businesses(.:format) {:action=>"index", :controller=>"businesses"}
POST /businesses(.:format) {:action=>"create", :controller=>"businesses"}
new_business GET /businesses/new(.:format) {:action=>"new", :controller=>"businesses"}
edit_business GET /businesses/:id/edit(.:format) {:action=>"edit", :controller=>"businesses"}
business GET /businesses/:id(.:format) {:action=>"show", :controller=>"businesses"}
PUT /businesses/:id(.:format) {:action=>"update", :controller=>"businesses"}
DELETE /businesses/:id(.:format) {:action=>"destroy", :controller=>"businesses"}
Rails doesn't really care where you have your forms so long as you provide it with the necessary information, & there's nothing that says you can't mingle various models together into a single view.
Assuming you're making use of RESTful resources(as you should), you'll have something like:
resources :pages
resources :companies
resources :clients
Setup in your routes.rb, this makes it pretty easy to specify how you want your form_fors to operate.
For instance on your show action for your Page model you could have something like:
<h1>New Company:</h1>
<%= form_for #company, :url => companies_path do |f| %>
...
<% end %>
<h1>New Client:</h1>
<%= form_for #client, :url => clients_path do |f| %>
...
<% end %>
Make sure you're setting the instance variables #company and #client in you pages controller show action like #company = Company.new & #client = Client.new.
In both of these cases your forms will post to the create action of their respective models. You can check out relying on record identification for further reading.
Related
I am attempting to create a new user in rails. There is a username, email, password and password confirm field on the new user form. When I click on the create user button on the web page, the new_user form simply refreshes and the user is not added to the database.
Here is the code for my register method in the authentication controller
def register
#user = User.new(params[:user])
if #user.valid?
#user.save
session[:user_id] = #user.id
flash[:notice] = 'Welcome.'
redirect_to sign_in_url
else
render :action => "new_user"
end
end
This is my new_user form:
<p>Sign Up</p>
<%= form_for #user, :as => :user, :url => new_user_path, :method => :put do |f| %>
<p>
<%= f.label 'username:' %><br/>
<%= f.text_field :username %>
<%= show_field_error(#user, :username) %>
</p>
<p>
<%= f.label 'email:' %><br/>
<%= f.text_field :email %>
<%= show_field_error(#user, :email) %>
</p>
<p>
<%= f.label 'password:' %><br/>
<%= f.password_field :password %>
<%= show_field_error(#user, :password) %>
</p>
<p>
<%= f.label 'password confirmation:' %><br/>
<%= f.password_field :password_confirmation %>
<%= show_field_error(#user, :password_confirmation) %>
</p>
<p>
<%= f.submit 'Sign Up' %>
<%= f.submit 'Clear Form', :type => 'reset' %>
</p>
Can anybody point me in the right direction?
Your call to new_user_path in your form references the url to User#new if you have defined resources :users in your routes.rb file. You will need to replace new_user_path with register_user_path, if you have defined register in your routes file. Double check for the path with by running rake routes, though.
From what I can see, there is no need to use have the register action. It'd make more sense to use RESTful actions new, create, index, show, edit, update, and destroy in your case, since your logic follows the intuition behind them. Rails makes this convention easy to follow. See the routing Rails guide here for more.
I want to be able to show the user that sent out the invitation rather than just my domain when sending out a devise invitation but haven't been able to find any documentation on this.
The two places where I need to show this name are in the invitation email-
(in place of 'Someone')
<p>Hello <%= #resource.email %>!</p>
<p>Someone has invited you to <%= root_url %>, you can accept it through the link below.</p>
<p><%= link_to 'Accept invitation', accept_invitation_url(#resource, :invitation_token => #resource.invitation_token) %></p>
<p>If you don't want to accept the invitation, please ignore this email.<br />
Your account won't be created until you access the link above and set your password.</p>
and the set password page.
<h4>You're seeing this page because someone has invited you to the site</h4>
<%= simple_form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => { :method => :put } do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :invitation_token %>
<div class="row">
<div class="signup_well span3 offset1">
<legend><%= t 'devise.invitations.edit.header' %></legend>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= hidden_field_tag :token_key, resource.invitation_token %>
<%= f.submit t("devise.invitations.edit.submit_button") %>
<% end %>
</div>
I may over looking any documentation on the best approach to do this. Your help saves a lot of frustration. Thank you.
This should works: <%= #resource.invited_by.first_name %>.
I am trying to render a partial which I have set up as the following. I have am also trying to create a nested form whereby I have included accepts_nested_attributes_for :user in my hospital_bookings model. I seem to be getting the following error:
NameError in Rota_days#index
Showing
C:/Users/home/Desktop/Portal/app/views/rota_days/index.html.erb
where line #31 raised:
undefined local variable or method `hospital_booking' for
which is pointing to the following line <%= render :partial => "booking_dialog", :locals => { :booking => hospital_booking.new } %> of my index.html.erb as shown below. I thought it was something to do with my pluralization. By changing hospital_bookings.new to hospital_booking.new but this did not work
_booking_dialog.html.erb
<%= form_for booking do |f| %>
<%= f.fields_for :user do |f| %>
<br/>
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.hidden_field :hospital_id %>
<%= f.hidden_field :id unless booking.new_record? %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
hospital_booking.new is nonsensical: you have no local variable named hospital_booking. If you want a new instance of the HospitalBooking model, then you want HospitalBooking.new.
So:
<%= render :partial => "booking_dialog", :locals => { :booking => HospitalBooking.new } %>
Update (from the comments)
In the booking_dialog form partial, you need to put the name attribute on the associated user record inside a fields_for block, to distinguish it from the fields for the parent (booking):
<%= form_for booking do |f| %>
<%= fields_for :user do |user_fields| %>
<%= user_fields.label :name %>
<%= user_fields.text_field :name %>
<% end %>
<%= f.hidden_field :hospital_id %>
<%= f.hidden_field :id unless booking.new_record? %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
p.s. it seems very strange that you have a hidden field for the :id in here. You shouldn't need that.
When the view pass the parameters to controller,
controller gets nil for all of the arguements somehow.
Can anyone how to fix this?? Thanks!
and I have no model called "Message"
controllers/messages_controller.rb
def deliver
recipient = User.find_by_username(params[:recipient])
subject = params[:subject]
body = params[:body]
current_user.send_message(recipient, body, subject)
redirect_to :controller => 'messages', :action => 'received'
flash[:notice] = "message sent!"
end
views/messages/new.html.erb
<%=form_for :messages, url: url_for( :controller => :messages, :action => :deliver ) do |f| %>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
<% end %>
Check your source HTML to better understand what FormHelpers do.
With the form_for f.text_field will generate names attributes in the format:
messages[subject]
Consequently, your params will be in the format:
params[:messages][:subject]
You can also use <%= debug params %> to inspect what's in params, it's very helpful.
You can get parameter value using datas = params[:messages]
These values are in array form. So you can fetch array datas If you want to individual data then usesubject = datas[:subject]
body = datas[:body]
To check run following code in view
<%= subject %>
this gives the value of subject.
Trying to make a nested form, which is working fine so far, except i need to put some dropdowns for the user to choose, as well as maybe make a couple of validations, however it seems nothing gets out of the form properly and keep getting errors no matter what I try.
three models.
--configuration
has_many :configoptions
accepts_nested_attributes_for :configoptions
--configoption
belongs_to :configuration
has_many :items
and item
belongs_to :configoption
scope :sorted, order('items.position ASC')
Now, so far I'm creating a nested form, looping through the configoptions, but for each option is possible there's more than one item. So I want to make a drop-down for those options where this is the case.
In my view i have:
<p>
<th>Elements</th>
<th>Quantity</th>
</p>
<%= form_for #config, :url => {:action => 'show', :id => #config.id} do |f| %>
<%= f.fields_for :configoptions do |fp| %>
<p>
<% if :items.count > 1 %>
<%= fp.text_field :name %>
<% else %>
<% fp.select(:items, :name)%>
<% end %>
<%= fp.text_field :quantity %>
</p>
<% end %>
<%= f.submit %>
<% end %>
I get an error obviously telling me that it can't count the :items.
How do you think I can make this work?
Thanks!
<%= form_for #config, :url => {:action => 'show', :id => #config.id} do |f| %>
<%= f.fields_for :configoptions do |fp| %>
<%= fp.text_field :id %>
<%= fp.text_field :name %>
<%= fp.text_field :quantity %>
<% end %>
<%= f.submit %>
<% end %>
OK, I think I figured it out, at least it seems to be doing what I want now.
I modified the view to pass the instance of the configoption into the nested form itself to be able to create the drop downs.
<% for configoption in #config.configoptions %>
<%= f.fields_for :configoptions, configoption do |fp| %>
<p>
<% if configoption.items.count > 1 %>
<%= fp.select (:name, options_from_collection_for_select(configoption.items.sorted, 'name', 'name'))%>
<% else %>