rails save select_tag value - ruby-on-rails

I'am using select_tag and when I update selected value is not stored
when i update :name, :tag value get first option
edit.html.erb
<%= form_for(#name) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :lname%><br />
<%= f.text_field :lname%>
</div>
<div class="field">
<%= f.label :tag %><br />
<%= f.select :tag, "<option>1</option><option>2</option><option>3</option><option>4</option>".html_safe %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Solution
<%= f.collection_select(:tag, [1,2], :to_i, :to_s, :prompt => 'Categories') %>

Maybe?
= f.select :tag, (1..4).to_a, :prompt => '---'

try to use select tag from this link
select_tag
<%= select_tag "tag", "<option>1</option><option>2</option>" %>

Try this
<%= f.select "tag", "<option value="1">1</option><option value="2">2</option></select>".html_safe %>
read http://ashleyangell.com/2009/11/form-select-helper-in-ruby-on-rails/

Solution
<%= f.collection_select(:tag, [1,2], :to_i, :to_s, :prompt => 'Categories') %>

Related

Ruby form_for html attributes not working, multipart or id

I am desperately trying to put a multipart to my form in Ruby but it won't show up. I looked up online everywhere but whatever I try it doesn't show. Even simple IDs or classes won't work...
Is there any dependency I am not aware of?
<%= form_for #listing, :html => {:id => "account_form", :multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
<%= f.label :highlights %>
<%= f.text_area :highlights, class: 'form-control' %>
<%= f.label :location %>
<%= f.text_area :location, class: 'form-control' %>
<%= f.label :catering %>
<%= f.text_area :catering, class: 'form-control' %>
<%= f.label :travel %>
<%= f.text_area :travel, class: 'form-control' %>
<%= f.label :dates %>
<%= f.text_area :dates, class: 'form-control' %>
<%= f.label :price %>
<%= f.text_field :price, class: 'form-control' %>
<%= f.label :category %>
<%= f.select :category, options_from_collection_for_select(Category.all, :id, :name), :include_blank => true %>
<%= f.label :country %>
<%= f.text_field :country, class: 'form-control' %>
<%= f.label :url %>
<%= f.text_field :url, class: 'form-control' %>
<%= f.label :photo %>
<%= f.file_field :photo %>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
which will result in the following HTML
<form class="new_listing" id="new_listing" action="/listings" accept-charset="UTF-8" method="post">
Please help!
It should change to multipart by itself when you add f.file_field call:
Using this method inside a form_for block will set the enclosing form’s encoding to multipart/form-data.
As was found out in comments, error was due to using #listing inside of partial (instead of local variable).
If you have _listing_form.html.erb partial, you should pass the local variables manually:
<%= render partial: 'shared/listing_form', locals: {listing: #listing} %>

Why won't my has_one relationship work

I have a few models Score ScoreAuthority ScoreColor and ScoreType. Score has_one authority, color, and type. Authority, color, and type each belong_to score. I am having trouble creating the form for Score.
I want users to create this relationship from Scores and everyone seems to handle only the reverse scenario creating from authorities, colors, types forms. What I mean specifically is for example:
Authorities
- foo
- bar
Colors
- red
- blue
I want the use to go into Score, chose to create a new score and then select foo from the drop down list, blue from the colors list, enter a score and submit. Authorities, colors and types will not change much and it would make any sense for the user to select Score from each of those.
When I currently do that I get the following error:
ScoreAuthority(#2248480380) expected, got String(#2155957040)
score/_form.html.erb
<%= form_for #score, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :product_id, "Products", :class => 'control-label' %>
<div class="controls">
<%= f.collection_select(:product_id, Product.order(:name), :id, :name) %>
</div>
</div>
<div class="control-group">
<%= f.label :score_authority, "Score Authority", :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :score_authority, ScoreAuthority.order(:name), :id, :name, {}, {:class=>'chosen'} %>
</div>
</div>
<div class="control-group">
<%= f.label :score_type, "Score Types", :class => 'control-label' %>
<div class="controls">
<%#= f.collection_select(:score_type, ScoreType.order(:name), :id, :name) %>
<%= f.collection_select :score_type, ScoreType.order(:name), :id, :name, {}, {:class=>'chosen'} %>
</div>
</div>
<div class="control-group">
<%= f.label :score_color, "Score Types", :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :score_color, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>
</div>
</div>
<div class="control-group">
<%= f.label :notation, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :notation, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :value, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :value, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
scores_path, :class => 'btn' %>
</div>
<% end %>
score_authorities, score_colors, score_types tables all have score_id as a column. Each of these models also has belongs_to :score.
The score model
has_one :score_authority
has_one :score_type
has_one :score_color
Not sure what I am doing wrong here, any help would be appreciated. Thanks!
you should use :score_authority_id instead of :score_authority in form
<%= f.collection_select :score_authority_id, ScoreAuthority.order(:name), :id, :name, {}, {:class=>'chosen'} %>
<%= f.collection_select :score_color_id, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>
<%= f.collection_select :score_color_id, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>
see this How do I create the view for the has_one association? for more detail
or you can try
class Score
has_one :score_authority
accepts_nested_attributes_for :score_authority
end
<%= form_for #score ... do |f| %>
...
<%= f.fields_for :score_authority do |b| %>
<%= b.collection_select :id, ScoreAuthority.all, :id, :name %>
...
<% end %>
...
<% end %>

rails, creates an object

Good Morning,
I have two scaffolds person and city.
rails g scaffold person :name, city_id
rails g scaffold city :cityname
and one formular view/people/
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :city_id %><br />
<%= f.text_field :city_id %>
</div>
<div class="field">
<%= f.label :cityname%><br />
<%= f.text_field :cityname%>
</div>
it doesnt go, i have to create an object of city and put the id in the city_id hidden_field and after that after create-button selected all should be saved in the database.
Not too hard or? Who would you make this?
No need to pass city_id.
<%= form_tag url_for(:controller => :your_controller, :action => :some_action, :method => :get do %>
<div class="field">
<%= label_tag :name %><br />
<%= text_field_tag :name %>
</div>
<div class="field">
<%= label_tag :cityname%><br />
<%= text_field_tag :cityname%>
</div>
<% end %>
In controller:
def some_action
city = City.find_or_create_by_cityname(params[:cityname])
person = Person.new(params[:name])
person.city_id = city.id
person.save!
end

Activeadmin form - Rails

How I can make this form in activeadmin?
<%= form_for(#album, :html => {:multipart => true}) do |f| %>
.....
<div class="field">
<%= f.label :apellido %><br />
<%= f.text_field :apellido %>
</div>
<div class="field">
<p>Hijos</p>
<%= f.fields_for :hijos do |builder| %><br /><br />
<%= builder.label :nombre, 'Nombre Hijo' %><br />
<%= builder.text_field :nombre %><br />
<%= builder.label :apodo, 'Apodo Hijo' %><br />
<%= builder.text_field :apodo %><br />
<%= builder.label :hijo_id, 'favorito' %>
**<%= f.radio_button :hijo_id, builder.object.id %>**
<% end %>
</div>
I need put the option of hijo_id inside the for of :hijos
Try with :
f.input :avatar_item_id, :as => :boolean, :value => app_f.object.id
But not work.
Thanks
This should work once you register the Album model as an ActiveAdmin resource:
form :html => {:multipart => true} do |f|
f.inputs "Principal" do
f.input :apellido
end
f.inputs "Hijos" do
f.has_many :hijos do |h|
h.input :nombre
h.input :apodo
h.input :favorito, :as => :check_box
end
end
f.buttons
end
If you want to mark a child as a favorite, you need a boolean favorito field in the hijos table, not a hijo_id field.

Problem with Ruby Gem Paperclip

I am having trouble using the Ruby Gem paperclip. I followed the instructions in the ReadMe but I cannot seem to get it to actually load my images. Here is my edit form:
<% form_for :user, #user, :html => { :multipart => true } do |f| %>
<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 :expertise %><br />
<%= f.text_area :expertise, :class => "expertise" %>
</div>
<div class="field">
<%= f.label :occupation %><br />
<%= f.text_field :occupation %>
</div>
<div class="field">
<%= f.label :city %><br />
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :state %><br />
<%= f.text_field :state %>
</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>
<%end%>
Yet when I try to save I keep getting this error: No route matches "/users/4/edit"
What is the problem
The error is telling you that there's no /users/4/edit route. What does your config/routes.rb look like? If there's a line like:
resources :users
Then, try changing that first line to:
form_for #user
Instead of:
form_for :user, #user
Also, I don't see a file_field in there anywhere so I don't think this question is about Paperclip?

Resources