This is what Things look like on my site.
I want "Cole Haan Air Madison" to actually link to the Cool Haan website where users can purchase that item.
This is the code for the form used to create Things:
<%= simple_form_for(#thing, html: { multipart: true }) do |f| %>
<% if #thing.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#thing.errors.count, "error") %> prohibited this thing from being saved:</h2>
<ul>
<% #thing.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :title %><br>
<%= f.text_field :title, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :description %><br>
<%= f.text_field :description, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :image %><br>
<%= f.file_field :image, class: 'form-control' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The form visually looks like this:
I know I have to add a link field to the form. But how do I, instead of displaying the link to the user, have it correspond to the title of the Thing?
Here is the show view for Things:
<div class='row'>
<div class='col-md-offset-2 col-md-8'>
<div class='panel panel-default'>
<div class='panel-heading text-center'>
<%= image_tag #thing.image.url(:medium) %>
</div>
<div class='panel-body'>
<p>
<strong><%= #thing.title %></strong>
</p>
<p>
<%= #thing.description %>
</p>
<p>
<%= link_to #thing.user.username, #thing.user %>
</p>
<% if #thing.user == current_user %>
<%= link_to edit_thing_path(#thing) do %>
<span class='glyphicon glyphicon-edit'></span>
<% end %>
<% end %>
</div>
</div>
</div>
Basically, you need link_to tag.
Since, you mentioned:
I want "Cole Haan Air Madison" to actually link to the Cool Haan
website where users can purchase that item.
So, I consider that you are storing website of the user's thing.
Replace,
<strong><%= #thing.title %></strong>
in your code with:
<strong><%= link_to #thing.title, #thing.user.website %></strong>
Here, #thing.user denotes the associated user of that thing, and #thing.user.website gives back the website of that user.
Related
I'm learning Rails and I try to use scaffold to generate some code.
Everything seems to be OK after this except I miss an input in one of my forms and I don't know why.
Someone could explain this ?
Here the partial form which generates the view:
_form.html.erb
<%= form_with(model: advertisement, local: true) do |form| %>
<% if advertisement.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(advertisement.errors.count, "error") %> prohibited this advertisement from being saved:</h2>
<ul>
<% advertisement.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_area :content %>
</div>
<div class="field">
<%= form.label :price %>
<%= form.number_field :price %>
</div>
<div class="field">
<%= form.label :state %>
<%= form.text_field :state %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
The HTML output for this input:
<div class="field">
<label for="advertisement_title">Title</label>
<input type="text" name="advertisement[title]" id="advertisement_title" />
</div>
Thank you everyone
OK I got it!
It's because of the Adblock extension.
On every page where it is enabled, it injects CSS directly into the page and adds a
display: none !important;
for hundreds of defined id's.
Unfortunately for me, #advertisement_title is one of them!
It works when I desactivate it
adblock display none
So I'm following a tutorial and near 1:07:39 in the New Review page, he has rating field and a comment field. But for me, only the comment page shows up, and I've tried copying his movie controller, reviews controller and the _form.html.erb. I am using ruby on rails and using the cloud 9 editor. All help is appreciated. If there's anything I have to add please comment.
This is my form:
<%= form_for([#movie, #review]) do |f| %>
<% if #review.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#review.errors.count, "error") %> prohibited this review from being saved:</h2>
<ul>
<% #review.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<div id="star-rating"></div>
</div>
<div class="field">
<%= f.label :comment %><br>
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<script>
$('#star-rating').raty({
path: '/assets/',
scoreName: 'review[rating]'
});
</script>
Your div for the rating is (probably) missing the label and text_field. You should do something like this:
<div class="field">
<div id="star-rating"></div>
<%= f.label :rating %><br>
<%= f.text_field :rating %>
</div>
I am trying to write an if else statement so that when a users post a photo only the photo is displayed the same goes for videos. Right now if a users post a video a blank photo will be displayed under the video as well. My code is below. I know it has something to do with the lines:
<%= image_tag #post.image.url(:medium) %>
<%= video_tag #post.video.url(:medium), controls: true, type: "video/mp4" %>
I just don't know what the best way is to only show the one the user is posting.
Post/Show.html
<div class="row">
<div class="col-md-offset-4 col-med-8">
<div class="panel panel-default">
<div class="panel-heading center">
<%= image_tag #post.image.url(:medium) %>
<%= video_tag #post.video.url(:medium), controls: true, type: "video/mp4" %>
</div>
<div class="panel-body">
<p><%= #post.description %></p>
<p><strong><%= #post.user.name if #post.user %></strong></p>
<% if #post.user == current_user %>
<%= link_to edit_post_path(#post) do %>
<span class="glyphicon glyphicon-edit"></span>
Edit
<% end %>
<% end %>
<%= link_to 'Back', posts_path %>
</div>
</div>
Form
<%= form_for #post, html: { multipart: true } 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 %>
<div class="field">
<%= f.label :image %>
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :video %>
<%= f.file_field :video %>
</div></br>
<div class="field">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit class: "btn btn-danger btn-md" %>
</div>
<% end %>
if i understand you correctly, you could try this...
<% if #post.respond_to?(:image) %>
<%= image_tag #post.image.url(:medium) %>
end
<% if #post.respond_to?(:video) %>
<%= image_tag #post.video.url(:medium), controls: true, type: "video/mp4" %>
end
You are basically checking for the existence of the particular attribute. You are saying, if it is here, if it exists, then display it.
Or you could try this
<% if #post.image.url != nil %>
<%= image_tag #post.image.url(:medium) %>
.........(repeat for other attributes)
<% end %>
or this
<% unless #post.image.url == nil %>
<%= image_tag #post.image.url(:medium) %>
<% end %>
see here for more info.
There are many ways to achieve what you want. this kind of thing is one of the fundamental concepts of programming.
I have a Simple Form and i just realized that if edit the form long enough (5-10secs) the Submit button suddenly doesn't work.
If i refresh the page and quickly type some letters and the submit it, it works...
This behavior is active on everything in my app that has a scaffold form, for example my Devise form's are just fine.
I have no idea what is causing this behavior.. What am i missing ?
EDIT
This behavior occurs only in Google Chrome Browsers.
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<h1>Test Form</h1>
</div>
</div>
<div class="panel-body">
<%= form_for(#question) do |f| %>
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :title %><br>
<%= f.text_field :title, class: "form-control", :autofocus => true,
placeholder: "#"%>
</div>
<div class="form-group">
<%= f.label :body %><br>
<%= f.text_area :body, class: "redactor redactor-box" %>
</div>
</div>
<div class="panel-footer">
<div class="form-group">
<%= f.submit "Post", class: "btn btn-primary" %>
</div>
</div>
<% end %>
</div>
It seems that the problem was in the HTML.
I wrapped all my html tags with form_for and for now it seems to be working..
I know you are usually supposed to use the linking between new/create and edit/update in rails, but I have a case where I need something else. Is there anyway I can achieve this same connection?
I have a form for a model and I want it to post the data (similar to how a new view, post to the create action).
Here is my form
<div id="new-job">
<%= form_for(#job) do |f| %>
<% if #job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% #job.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 :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit "create job", id: "submit-job", class: "button small radius" %>
<%= link_to "go back", jobs_path, class: "button small radius secondary" %>
<div id="new-job-errors"> </div>
</div>
<% end %>
</div>
Use the :url option.
= form_for #job, :url => company_path, :html => { :method => :post/:put }