Bootstrap-form gem, strange error - ruby-on-rails

Here is the code for a form created with the bootstrap-form gem for rails.
<%= bootstrap_form_tag(user_sessions_path) do |f| %>
<div class="field">
<%= f.label_tag :email %><br />
<%= f.text_field_tag :email %>
</div>
<div class="field">
<%= f.label_tag :password %><br />
<%= f.password_field_tag :password %>
</div>
<div class="actions">
<%= f.submit_tag "Login" %>
</div>
<% end %>
This throws an error: no implicit conversion of symbol to string on the first line of the form containing the user sessions path. Not sure why this is happening

I believe you have to be explicit about the url. Like this:
<%= bootstrap_form_tag(url: user_sessions_path) do |f| %>
<div class="field">
<%= f.label_tag :email %><br />
<%= f.text_field_tag :email %>
</div>
<div class="field">
<%= f.label_tag :password %><br />
<%= f.password_field_tag :password %>
</div>
<div class="actions">
<%= f.submit_tag "Login" %>
</div>
<% end %>
Check the source code. You can see as how the bootstrap_form_tag method, expects a Hash parameter:
def bootstrap_form_tag(options = {}, &block)
options[:acts_like_form_tag] = true
bootstrap_form_for("", options, &block)
end

Related

Rails 3.2 is not saving dropdown selection in form after clicking edit

I have a form with input field and dropdown boxes with data from other tables. (joins)
but now when i submit it is good, but when i want to edit the fields the dropdown fields are empty but the text fields aren't
My form code:
<%= form_for #contracten, :html => { :multipart => true } do |f| %>
<% if #contracten.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#contracten.errors.count, "error") %> prohibited this contracten from being saved:</h2>
<ul>
<% #contracten.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="forms">
<div class="field">
<%= f.label :naam %><br />
<%= f.text_field :naam %>
</div>
<br>
<div class="field">
<%= f.label :omschrijving %><br />
<%= f.text_area :omschrijving %>
</div>
<br>
<div class="field">
<%= f.label :organisatie %><br />
<%= f.select :organisatieid, options_for_select(#organisaties.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :vestiging %><br />
<%= f.select :vestigingid, options_for_select(#vestigingens.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :Beheerder %><br />
<%= f.select :persoonid, options_for_select(#personen.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :contractsoort %><br />
<%= f.select :contractsoortid, options_for_select(#contractsoorten.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :datumingang %><br />
<%= f.datetime_select :datumingang %>
</div>
<br>
<div class="field">
<%= f.label :datumeinde %><br />
<%= f.datetime_select :datumeinde %>
</div>
<br>
<div class="field">
<%= f.label :contractduur %><br />
<%= f.number_field :contractduur, :placeholder => 'in jaren' %>
</div>
<br>
<div class="field">
<%= f.label :opzegtermijn %><br />
<%= f.number_field :opzegtermijn, :placeholder => 'In Maanden' %>
</div>
<br>
<div class="field">
<%= f.label :betalingsperiode_eenheid_id %><br>
<%= f.select :betalingsperiodeeenheidid, options_for_select(#betalingsperiodeeenhedens.map{ |f| [f.omschrijving, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :betalingstermijn %><br />
<%= f.number_field :betalingstermijn %>
</div>
<br>
<div class="field">
<%= f.label :pdf %><br />
<%= f.file_field :pdf %>
</div>
<br>
<div class="actions">
<%= f.submit "Toevoegen", :class => 'button3' %>
</div>
</div>
<% end %>
<br>
screenshot:
I hope someone know's what the problem.
Add the :selected => ... to your options_for_select parameters.
E.g:
<%= f.select :contractsoortid,
options_from_collection_for_select(#contractsoorten, 'id', 'naam',
:selected => #contracten.contractsortid),
:include_blank => true %>
The changes are actually saved, but when you render the form, the actually selected value must be specified.

No method error in Users#edit

I am learning ruby on rails. I want to make editable users account.
I have an error in localhost:
undefined method `model_name' for NilClass:Class
This is my edit.html.erb file:
<h1>Edit user</h1>
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<div>
<%= gravatar_for #user %>
change
</div>
Can you provide a solution?
Most likely your #user object is nil, so when it tries to call #user.model_name, it fails. You should check your controller logic for loading it, and make sure it finds a valid #user.

How to create a HTTP post request using the form fields to the external websites API using Ruby on Rails?

I am trying to create a HTTP post request using the form fields to the external website here it is factual.com
I have created the method in the my data_controller.rb which is like
def posttofactual
uri = URI.parse("http://api.factual.com/v2/tables/Nj0JN3/input?")
# Full control
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"name" => "datum.name", "address" => "datum.address", "locality" => "datum.locality", "region" => "datum.region", "postcode" => "datum.postcode","category" => "datum.category","website" => "datum.website","latitude" => "datum.latitude","longitude" => "datum.longitude","&APIKey=" => "myapikey" })
# Tweak headers, removing this will default to application/x-www-form-urlencoded
request["Content-Type"] = "application/json"
response = http.request(request)
end
I have a form like
<%= form_for(#datum) do |f| %>
<% if #datum.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#datum.errors.count, "error") %> prohibited this datum from being saved:</h2>
<ul>
<% #datum.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_field :address %>
</div>
<div class="field">
<%= f.label :locality %><br />
<%= f.text_field :locality %>
</div>
<div class="field">
<%= f.label :region %><br />
<%= f.text_field :region %>
</div>
<div class="field">
<%= f.label :postcode %><br />
<%= f.number_field :postcode %>
</div>
<div class="field">
<%= f.label :category %><br />
<%= f.text_field :category %>
</div>
<div class="field">
<%= f.label :website %><br />
<%= f.text_field :website %>
</div>
<div class="field">
<%= f.label :latitude %><br />
<%= f.text_field :latitude %>
</div>
<div class="field">
<%= f.label :longitude %><br />
<%= f.text_field :longitude %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I want to call "posttofactual" method on this line <%= f.submit %> on the form field.
It seems like your form will post to either create or update method of the controller. There you should extract data from params and call your posttofactual method.

Associated model is not saving data when page is refreshed

Rails 3.1 RC4
I have a 1:1 association between User and Profile.
When I submit the new profile form, the data I've entered is displayed just fine (see screenshot: http://i.imgur.com/fY8YU.png), but when I refresh it the data is instantly wiped.
Could anyone tell me what is causing this?
Here's the submit form:
<%= form_for([#user, #user.build_profile]) do |f| %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :picture %><br />
<%= f.text_field :picture %>
</div>
<div class="field">
<%= f.radio_button(:sex, "male") %>
<%= f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female") %>
<%= f.label(:sex, "Female") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here's the users_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/users_controller.rb
Here's the profiles_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/profiles_controller.rb
I'm not sure I agree with your approach. Why don't you do something like this:
In models/user.rb:
accepts_nested_attributes_for :profile
In controllers/users_controller.rb:
def new
#user = User.new
#user.build_profile
end
In views/users/_form.html.erb:
<%= form_for #user do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :profile do |pf| %>
<%= pf.text_field :some_profile_field %>
<% end -%>
<%- end -%>
This isn't copied or tested, but it should work. On saving your user, the profile fields are sent along and validated with the user fields and are re-rendered when rendering the form again after a save error. This way you will keep full control over your form and its contents with minimal effort.

Rails 3 Nested Forms

I have a Person model and an Address Model:
class Person < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
end
class Address < ActiveRecord::Base
belongs_to :person
end
In my people controller I have #person.build_address in my new action. My forms builds correctly. The problem is that when I submit the form, a person record and an address record is created but they aren't linked via the address_id column in the Person table.
Am I missing a step in the controller?
Thanks!
New Action
UPDATE
def new
#person = Person.new
#person.build_address
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #person }
end
end
Form Code
UPDATE
<%= form_for(#person) do |f| %>
<% if #person.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#person.errors.count, "error") %> prohibited this person from being saved:</h2>
<ul>
<% #person.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :telephone %><br />
<%= f.text_field :telephone %>
</div>
<div class="field">
<%= f.label :mobile_phone %><br />
<%= f.text_field :mobile_phone %>
</div>
<div class="field">
<%= f.label :date_of_birth %><br />
<%= f.date_select :date_of_birth %>
</div>
<div class="field">
<%= f.label :gender %><br />
<%= f.select(:gender, Person::GENDER_TYPES) %>
</div>
<div class="field">
<%= f.label :notes %><br />
<%= f.text_area :notes %>
</div>
<div class="field">
<%= f.label :person_type %><br />
<%= f.select(:person_type, Person::PERSON_TYPES) %>
</div>
<%= f.fields_for :address do |address_fields| %>
<div class="field">
<%= address_fields.label :street_1 %><br />
<%= address_fields.text_field :street_1 %>
</div>
<div class="field">
<%= address_fields.label :street_2 %><br />
<%= address_fields.text_field :street_2 %>
</div>
<div class="field">
<%= address_fields.label :city %><br />
<%= address_fields.text_field :city %>
</div>
<div class="field">
<%= address_fields.label :state %><br />
<%= address_fields.select(:state, Address::STATES) %>
</div>
<div class="field">
<%= address_fields.label :zip_code %><br />
<%= address_fields.text_field :zip_code %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You need to have accepts_nested_attributes_for :address on your Person model for this to work nicely. In your create action you can then do this:
def create
#person = Person.new(params[:person])
...
end
Then Rails will take care of the rest.
UPDATE: if the address_id column is in the people table then it should be belongs_to :address, not has_one :address
Why is your address built in your new action, and not in the create action? You're building an address from a non saved model, without an id, so the foreign key can't be set. You should keep your #person in your new action, but put your build_address in your create action, after #person has been saved.

Resources