Supply information automatically to simple form - ruby-on-rails

I'm working on a simple little Rails social network and right now I am trying to automatically fill in the name of the currently signed in user when creating a new status update. My original code let them select their user name from a drop down which is silly - when you go on Facebook, you fill out the box and it automatically knows that it belongs to you. Here is my original code:
<%= simple_form_for(#status, html: {:class => "form-horizontal"}) do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this status from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id, collection: User.all, label_method: :full_name %>
<%= f.input :content %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
And here is what I am trying to replace it with:
<%= simple_form_for(#status, html: {:class => "form-horizontal"}) do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this status from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id, current_user.full_name %>
<%= f.input :content %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
It doesn't like this and says "can't convert Symbol into Integer"
How can I supply it with the logged in user's full name without ever giving them the option to chose another user?

user_id is the forreign key to User, it holds something like current_user.id not it's name.
It seems, that the user is already signed in, so you just need to show the name and provide the id to the create action:
<%= f.input :user_id, as: :hidden %>
<%= current_user.name %> <%# just for information %>
and set it in the new #status in the controller:
def new
#status = Status.new(user: current_user)
...
end

Your first input of the form is specified as an input for the :user_id which is an integer in your table of users. Putting a full name there is creating a mismatch in data types. Does this help?
Now that you want the username to be displayed without the user being able to change what is displayed, is there really a need to have this as an input? Can you just display the username in a normal <div></div>, so to speak?

Related

Create several records at once in Rails

In my app users can create a set of codes to used in marketing activities. Therefore I want a form in which a user can type the amount of codes he want to create and then the system should generate that many codes and save them to the database.
code.rb
class Code < ActiveRecord::Base
belongs_to :form
attr_accessor :amount
end
_form.html.erb
<%= form_for [:customer, #code] do |f| %>
<% if #code.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#code.errors.count, "error") %> prohibited this code from being saved:</h2>
<ul>
<% #code.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :amount %><br>
<%= f.text_field :amount %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
What I want to do now is to tell the controller:
take :amount
create as many SecureRandom.hex(3) as :amount specifies
save all codes to database (column for the codes in the table is named "code")
However, I totally don't know how to achieve this...

Rails form validation not show the error messages, taking blank fields

My form::
<h1>New Post</h1>
<%= form_for (#post), url: posts_path do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :Title%><br>
<%= f.text_field :Title%>
</p>
<p>
<%= f.label :Text%><br>
<%= f.text_area :Text%>
</p>
<p>
<%= f.submit :Save %>
</p>
<% end %>
:: context ::
It is not showing the error messages. please point out the where i am wrong. I want to add basic validations. i have gone through some tutorials but still stuck into this.. please help me out.
In your Post model,Just add these line
validates_presence_of :title,text #doesn't allow the blank values
If you are also looking for validation of unique values for title,add this line also.
validates_uniqueness_of :title #doesn't allow duplicate values
Also Follow these Guides(Rails 4),if you are looking for more validations.
validates :title, uniqueness: {message: "must be uniq"}

Passing User_ID through new_item form

I have a simple app that allows users to create 'items'. On the _form, the only data that it asks for is 'content' and 'user_id', which is currently a number picker that assigns user_id to the item for ownership. But what I want to do is have the form assume that the user_id is the current user's ID (using Devise). That way other people can't assign 'items' to other users. Make sense? Here's the form.
_form.html.erb
<%= form_for(#item) do |f| %>
<% if #item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% #item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You should be working with associations from the model.
example your Items model should have belongs_to :user
then your would just use the :user method not the user_id attribute.
But you really could make this much more simpler.
install simple_forms gem it will make your life easier.
gem "simple_form"
then
<%= simple_form_for(#item) do |f| %>
<%= f.input :content %>
<%= f.association :user %>
<%= f.button :submit %>
<% end %>

rails Insert meta data from external url into table

I have a table for storing links. I'm trying to use the pismo gem for grabbing meta data from a url, and saving that to the table.
In my link controller I have a before filter :
def pismo_grab_meta_data
doc = Pismo::Document.new("http://google.com/")
#link_title = doc.title
end
Which gets called in the new and create action. In my view I pass the instance variable in a hidden field.
<%= f.hidden_field :name, :value => #link_title %>
This works when hard code the url as a string (like the above google example), but fails when I pass it the url params, like so:
doc = Pismo::Document.new(params[:url])
Error: undefined method `gsub!' for nil:NilClass
Pretty new to rails, so I assume I'm doing something stupid here. Any help is very welcome!
<%= form_for(#link, :html => { "data-behavior" => "submit-form"}) do |f| %>
<% if #link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#link.errors.count, "error") %>
prohibited this link from being saved:
</h2>
<ul>
<% #link.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :name, :value => #link_title %>
<div class="field urlsaver">
<%= f.label :url %><br />
<% if params[:link].present? %>
<%= f.text_field :url, :value => params[:link] %>
<% else %>
<%= f.text_field :url %>
<% end %>
</div>
<div class="actions">
<%= f.submit 'Save', :class => 'btn' %>
</div>
<% end %>
The value you are looking for should be in params[:link][:url].
Change the line to :
doc = Pismo::Document.new(params[:link][:url])

call form rails with bootstrap twitter

Someone has used to the modal of twitterboostrap calling to _form.html.erb ?
This is my try.
first I add the next line in the form (this is other form)
<%= simple_form_for(#terreno, html: {class: "form-horizontal"}, remote: :true) do |f| %>
<% if #terreno.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#terreno.errors.count, "error") %> prohibited this terreno from being saved:</h2>
<ul>
<% #terreno.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :titulo %>
<%= f.input :direccion %>
<%= f.input :region %>
<%= f.input :metrocuadrado %>
<%= f.input :precio %>
<%= f.input :votos %>
<%= link_to "Add image", new_image_path, id: "new_image", class: 'btn', remote: :true %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
The next step has been create the view for image. in this Case the view is view/images/new.js.erb
In this file I add the next code:
$('#new_image').hide().after('<%= j render("form") %>')
When I clicked the button "Add image" no show the form, but when I show the console(firebug), this show me the content of the _form.html.erb (remember this form belongs to image controller).
Any idea ?
Pdt: My english is very bad, sorry.
This may be down to HTML escaping of characters. Try:
$('#new_image').hide().after('<%= escape_javascript(raw render :partial => "form").html_safe %>');

Resources