Currently I have a model Abc. It has a column in it's associated database called 'description'. I'm trying to figure out how to populate a dropdown menu with the contents of the description so that the user would be able to select through each of the choices and pick one.
Currently, my action in my controller has this:
#abc = Abc.all
As far as I understand, this should select all records in the database that is associated with the model Abc.
Then, in the view associated with this action, I have this:
<select name="id">
<%= #abc.each do |abc|%>
<option value="<% abc.id %>"><%= abc.description%></option>
<% end %>
The above is something I was trying to adapt off an example I found online, which isn't working. I think this is supposed to create a dropdown menu with option values set to ID of the database entries. Then the actual content of the dropdown menu is populated with the contents of the 'description' column of the database. This isn't working as of now. Currently giving me this error:
undefined method `each' for nil:NilClass
on the:
<%= #abc.each do |abc|%>
line of code.
The example I was talking about is here:
<select name="user_id">
<%= #users.each do |user| %>
<option value="<%= user.id %>"><%= user.name %></option>
<% end %>
</select>
The context of the example is here: http://www.theodinproject.com/ruby-on-rails/advanced-forms
I'm pretty new to rails, any help would be appreciated. I get the feeling that I'm misunderstanding something about rails itself since the 'each' method is rails' version of an iterator.
Related
I have an application I want to use for my teaching studio where I create lessons that students can view. the lessons are specific to each student so they are linked via a :user_id (user has_many lessons, lesson belongs_to user). I can bring up specific lessons based on which user it is but the one part I cannot figure out is how to get a video (youtube) link to display correctly. What I want is to save the link along with the text for the lesson. The text and title and all that comes up fine right now, but the video will not display with embed code and throws a routing error in the iframe window.
Here is my code for the _lesson partial. "video_url" is the name of the column in the database.
_lesson.html.erb
<li>
<span class="content">
<%= lesson.title %><br />
<%= lesson.lesson_notes %>
<iframe width="490" height="275" src="<%= lesson.video_url %>" frameborder="0" allowfullscreen></iframe>
</span>
</li>
this is the code in my show view.
show.html.erb
<% if #user.lessons.any? %>
<h3> Lessons (<%= #user.lessons.count %>)</h3>
<ol class="lessons">
<%= render #lessons %>
</ol>
<%= will_paginate #lessons %>
<% end %>
the error I get is below
No route matches [GET] "/users/www.youtube.com/embed/CFF0mV24WCY"
I kind of understand what is happening, but not really. I guess I am unsure why it is looking for that route instead of just embedding the code and then displaying the video. It brings up all the other text and stuff from the lesson so that is working fine. Is there not an easy way just to display the link so it embeds the video along with it? I have searched quite a bit but everything I find just deals with embedding the code into the html which I can do, but this needs to change based on the user so has to be pulled out of the database along with the rest of the lesson info. Any help would be greatly appreciated.
src needs to know the protocol, so if you don't include http://, the explorer assumes that the given src is next to your actual path.
A shitty solution could be add the protocol with a raw string:
src="http://<%= lesson.video_url %>"
But a way better solution is adding a filter to check if the protocol is included when you save the field.
When I use Simple Form it shows three different select boxes for column type date. Instead of this I want Simple Form to show date attributes as HTML5 does. Specifically I want Simple Form to do the following:
<input type="date" name="org[established_at]" id="org_established_at">
For this I tried:
<%= f.input :established_at, as: :date %>
But this produces three different select boxes for date picking.
How do I tell Simple Form and Rails 4 to use input type "date"?
Recently I've done it like this:
f.input :established_at, as: :string, input_html: { class: :datepicker }
and used http://xdsoft.net/jqplugins/datetimepicker/ to show the calendar
In my project I used next:
= f.input_field :year, as: :datepicker, class: "string form-control teacher_kpk_year", readonly: true
Gemfile:
gem 'bootstrap-datepicker-rails'
applications.js
//= require bootstrap-datepicker
If you have a field defined as date, you only need add the parameter html5 like this:
f.input :established_at, html5: true
But, if you haven't defined your field like date or something like that, you only need to add the alias to the previous declaration, like this form:
f.input :established_at, as: :date, html5: true
And both forms will render a date or datetime picker like bootstrap-datepicker
best regards,
I am new to rails and am looking to do something very simple but don't know the correct syntax. I have 2 buttons one for creating a new page and one for creating a new post. Both a page and a post save in the same database table and are controlled through a boolean field called 'static'. A page therefore has a static value of 1 and a post 0. What I want to do is simply auto set this value in a form (and hide it) when I click new page or post. I imagined the link to create a new page would work something like this:
<%= link_to 'New Page', new_page_path(:static => "1") %>
This doesn't work so I tried to create a new_static page action and a new_post page action with correcting routing (for displaying only pages I created a show_static action used the following link_to and it works fine):
<%= link_to "Pages", show_static_pages_path(#pages), :method => :get %>
The problem is when I created the new_static page action it expects an id for some reason.
new_static_page GET /pages/:id/new_static(.:format) pages#new_static
I would prefer to not mess around with new actions and routing and simply set the value with link_to. Any tips would be greatly appreciated.
I'm trying to add a show page to my devise users but can't seem to get the id value passed properly, how do I create a link to the show page? For some reason rails is doing this to URL
/show.1
users controller:
def show
#user = User.find(params[:id])
end
user/show.html.erb
<h1><%= #user.username %></h1>
link to user profile also gives me issues id no method
<%= link_to #post.email, users_show_path(#user.id) %>
routes.rb
get "users/show"
get "pages/faq"
get "pages/about"
devise_for :users
resources :posts
You have defined the show route as:
get "users/show"
Notice that there is no dynamic segment in this route, like :id. That means your route is not expecting any value to be passed. BUT while linking to this route you passed #user.id
<%= link_to #post.email, users_show_path(#user.id) %>
which the route was not expecting. So, Rails assumed that you are passing the format (like .html, .json, etc.) which is why you see the url formed as users/show.1.
To fix this, I would suggest you to add a dynamic segment to your route to capture the id of a user correctly.
Change
get "users/show"
With
get "users/show/:id" => "users#show", as: :users_show
For a user with id = 1, when you click on the user profile link the url generated would be http://yourdomain/users/show/1
Using standard sitecore controls (Link and Text), you can embed a text field within a link in the following way:
<sc:Link runat="server" Field="LinkUrl" >
<sc:Text runat="server" Field="LinkText" />
</sc:Link>
That will give you the ability to edit the text of one field and link of another field.
I have tried to replicate this using Glass, but have been unsuccessful. Something like this would be good (It doesn't work):
<%= Editable( x => x.LinkUrl,new { Text = Editable(Model,q => q.LinkText,null)}) %>
Is there another way of sorting this out?
There are two options I see if I cannot do this using standard glass functionality:
Change the GlassHtml code
Use Two Fields
If you are using Razor use this:
#using (BeginRenderLink(x => x.Link, isEditable: true))
{
#Editable(x => x.Title);
}
If you are using WebForms:
<%using(BeginRenderLink(x=>x.Link){ %>
<%=Editable(x=>x.Title) %>
<% } %>
Mike
If you are using Glass version 3, then you can't use Editable on Link and Image fields.
Use RenderLink and RenderImage instead.
See here: http://glass.lu/docs/tutorial/sitecore/tutorial22/tutorial22.html
The Editable method is the most basic method to use to make a field
editable and should be used with most fields that are page editable
except the Image field and General Link field