Rails link_to issues - ruby-on-rails

I am trying to make my rails application generate a link without the text and custom content instead. I am currently doing this
<%= link_to '', {:controller => :project, :action => :show}, {:id => project.id, :class => "projects"} do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>
Which generates this code
<a action="show" controller="project" href>
<div class="image-container">
<img alt="House 2" src="/uploads/project_image/image/1/house-2.jpg" title="Ygfaweg">
</div>
<p>
<span class="title">
</span>
<span class="summary">
</span>
</p>
</a>
The link is incorrect and is missing alot of data, any idea why?

Remove '' from link_to method.
Change :controller in link_to method to projects
Remove braces from :id => project.id, :class => "projects"
So, refactor the first line to:
<%= link_to {:controller => :projects, :action => :show}, :id => project.id, :class => "projects" do %>

I didnt come to a pure conclusion with this issue. I ended up doing this which works fine.
<a class="project" href="<%= url_for({:controller => :project, :action => :show, :id => project.id}) %>">
Cheers everyone!

Try to do like this :
<%= link_to ({:controller => :project, :action => :show,:id => project.id}, {:class => "projects"}) do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>
Hope this will help you.

Just remove blank string from link_to and you are done.
<%= link_to {:controller => :project, :action => :show}, {:id => project.id, :class => "projects"} do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>

Related

Customize Bootstrap Form In Rails View

I can't figure out the proper formatting to modify my form partial in my view to look like the bootstrap form below with a appended button like this;
<div class="col-lg-9">
<div class="input-group input-group-lg">
<input type="text" class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div><!-- /input-group -->
I would like the form to post to :home and the appended button to replace the default f.submit button in a Scaffold. Here's a Scaffold form partial that I'd like to modify.
<%= form_for #anesu, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :home, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :home, :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")),
anesus_path, :class => 'btn' %>
</div>
<% end %>
Please guide me on how to format my code and the right methods to use
<%= form_for #anesu do |f| %>
<div class="col-lg-9">
<div class="input-group input-group-lg">
<%= f.text_field :home, :class => 'form-control' %>
<span class="input-group-btn">
<%= f.submit "Go!", :class => 'btn btn-primary' %>
</span>
</div>
</div>
<% end %>

How to write this in ruby on rails?

<div id="datetimepicker" class="input-append date">
<input type="text"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
This is what I could come up with. And it was wrong!
<%= text_field_tag('datetimepicker',
content_tag(:span,
content_tag(:i,
{data-time-icon:'icon-time'},
{data-date-icon:'icon-calendar'}
),
{class: 'add-on'}
),
{class: 'input-append'}
)%>
Any help would be greatly appreciated.
text_field_tag is for input, for other you can use content_tag
This is your current markup:
<div id="datetimepicker" class="input-append date">
<input type="text"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
Using Rails helpers it can be modified to :
<%= content_tag :div, :class=> "input-append date", :id => 'datetimepicker' do %>
<%= text_field_tag '' %>
<%= content_tag :span, :class => "add-on" do %>
<%= content_tag :i, :data => {:time-icon => 'icon-time', :date-icon => 'icon-calendar'} %>
<% end %>
<% end %>
However I prefer Basic HTML over these ActionView Helpers, unless its helping out with code, which in this case is not much..

How to avoid html tags in edit page

I am using rails 4 and I have used jquery text-editor. For omitting html tags, I have used sanitize helper.it's working fine.But edit the form shows html tags in description field.
form.html.erb
<%= simple_form_for #newsletter, html: {class: 'form-inline form-horizontal'}, :validate => true do |f|%>
<br/></br/><br/>
<div class="tabable well">
<div class="tab-content">
<div class="tab-pane active">
<div class="inputs">
<h4 class="" style="border-bottom: 1px solid #dcdcdc;"> <img src="/assets/side_menu/news-and-events.png" alt="Home" ></i> Add NewsLetter</h4><br/><br/>
<div class="offset0">
<%= f.input :name %>
<%= f.input :description, as: 'text', :input_html =>{:rows => '20', :cols => '180', :class => 'wysiwyg input-block-level' }%>
</div>
</div>
<div class="form-actions">
<%= button_tag(type: 'submit', class: "btn btn-primary") do %>
<i class="icon-ok icon-white"></i> Save
<% end %>
<%= link_to new_newsletter_path, class: 'btn btn-inverse' do %><i class='icon-hand-left'> Cancel</i><% end %>
</div>
</div>
</div>
<% end %>
Edit Page
edit.html.erb
<%= render 'news/sidebar'%>
<%= link_to 'Back', newsletters_path, :class => 'btn btn-inverse animated rotateIn' %>
<%= link_to 'View', newsletter_path(#newsletter), :class => 'btn btn-success animated rotateIn pull-right' %>
<br><br>
<div class='row-fluid clear'>
<div class='box gradient'>
<div class=''>
<%= render 'form' %>
</div>
</div>
</div>
I don't know , how to use sanitize in edit page. Please Guide Me..
it appears you are missing a closing </div>

Rails how to update image? And how to do RESTful routing?

I am trying to update my photographer model. But how problems with Rails routing and paperclip wont replace image.
When I submit my form the url is: http://localhost:3000/admin/photographers/save.75 which gives an error. Couldn't find Photographer without an ID
And my image is not updated.
My form:
<%= simple_form_for #photographer, :url => save_admin_photographers_path(#photographer), :html => {:multipart => true, :method => :post} do |f| %>
<%= javascript_include_tag "tiny_mce/tiny_mce" %>
<%= javascript_include_tag "init_my_tiny_mce" %>
<%= f.input :name, :label => 'Name' %>
<%= f.text_area :text, :label => 'Text', :size => '12x12' %>
<%= f.file_field :image, :label => 'Image' %>
<% if #photographer %>
<% if #photographer.image %>
<p class="ok">
<label for="dd">image ok</label>
<%= image_tag("http://s3-eu-west-1.amazonaws.com/kjacobsen/photographer/image/#{#photographer.id}/#{#photographer["image"]}", :size => "39x22") %>
</p>
<p>
<label for="sdd"> </label>
<%= link_to("remove", {:action => "remove_image", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No image uploaded for movie
</p>
<% end %>
<br />
<% end %>
<br />
<%= f.file_field :flv, :label => 'Upload FLV' %>
<br />
<% if #photographer %>
<% if #photographer.flv %>
<p class="ok">
<label for="dd">flv: <%= #photographer["flv"] %></label>
<%= link_to("remove", {:action => "remove_flv", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
No flv uploaded
<br /><br />
</p>
<% end %>
<% end %>
<br />
<%= f.file_field :quicktime, :label => 'Upload Quicktime' %>
<br />
<% if #photographer %>
<% if #photographer.quicktime %>
<p class="ok">
<label for="dd">quicktime: <%= #photographer["quicktime"] %></label>
<%= link_to("remove", {:action => "remove_quicktime", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No quicktime uploaded
<br />
</p>
<% end %>
<% end %>
<%= f.button :submit, :value => 'Create movie' %>
<% end %>
My update controller:
def save
#photographer = Photographer.find(params[:id])
#photographer.update_attributes(params[:photographer])
if !#photographer.save
flash[:notice] = " "
render_action 'edit'
else
flash[:notice] = "update ok"
redirect_to :action => 'edit', :id => #photographer.id
end
end
My routes:
namespace :admin do
resources :photographers do
collection do
post :save
end
end
end
What you're doing is basically an update action. The update route is automatically created when you do resources :photographers, you can verify this by typing rake routes in the terminal.
You should rename your controller action from save to update and remove the custom route:
namespace :admin do
resources :photographers
end
Then use the update route in your form:
:url => admin_photographer_path(#photographer)
You should also change your html method, the update action uses PUT:
:method => :put
Hope this helps.

Rails form submit problem having 2 forms

I have that problem it is always the same form that gets submitted.
The update_limits action gets called on the update order submit button. Which should trigger the action update_order.
Here is my view:
<h2>Movies</h2>
<h3>List movies</h3>
<%= form_tag(:action => 'update_limits' ,:id => params[:id]) %>
<%= link_to 'create new movie', {:action => 'create',:id => params[:id]}, {:class => 'margin-left'} %>
<div class="spacer"> </div>
Number of movies in reel:
<span class="c1">
<% rr = 1..6 %>
<%= select("limits", "reel_limit", rr) %>
</span>
Number of movies in archive:
<span class="c1">
<% rr = 0..12 %>
<%= select("limits", "archive_limit", rr) %>
</span>
<%= submit_tag %>
<div class="spacer"> </div>
<%= form_tag(:controller => 'admin/photographers', :action => 'update_order' ,:id => params[:id]) %>
<ul id='movielist'>
<span class="header">name</span>
<%
n = 0
while n < #items.length
%>
<li itemID='<%=#items[n].id%>' <%= reel_color_class(n, #limits) %>>
<% if #items[n]["image"] %>
<%= image_tag("/photographer/image/#{#items[n].id}/#{#items[n]["image"]}", :size => "36x20" ) %>
<% end %>
<%=#items[n].name.force_encoding("UTF-8") %>
<span class='col2'>
<%= link_to 'edit', {:action => "edit", :id => #items[n].id} %>
<%= link_to("remove", {:action => "remove", :id => #items[n].id },
{:confirm => "Are your sure?"}) %>
</span>
</li>
<%
n = n + 1
end
%>
</ul>
<input type="hidden" name="neworder" id="neworder" value="" />
<input name="commit" type="submit" value="update order" onclick="neworder.value=(junkdrawer.inspectListOrderNew('movielist'))" />
<div class="spacer"> </div>
The form_tag method takes a block, and you are not giving it one. You should be doing something like this:
<%= form_tag(:action => 'update_limits' ,:id => params[:id]) do %>
# form goes here
<% end %>
Or even better, if this is acting on a real object, using a form_for tag:
<%= form_for(#object) do |f| %>
# form_goes here
<% end %>
For more information, please read the Getting Started guide for Rails.

Resources