I have a model called "ExpDemo" and want to use it from "MainController."
I setup the code like this:
main_controller.rb
def pre
#demo = ExpDemo.new
end
main/pre.html.erb
<% form_for(#demo) do |f| %>
...
<% end %>
Until here, I experienced 'path' error.
undefined method `exp_demos_path'
So, I added following to routes.rb and the error message gone.
resources :exp_demos
Now, the form is not showing up in the HTML page.
I think the routing setup is the problem, but I'm not sure how to fix it.
Please help me to resolve this issue.
you forgot an = for the form_for
<%= form_for(#demo) do |f| %>
Solution:
Change <% form_for to <%= form_for
Explanation:
<% %> Executes ruby code that doesn't output results
<%= %> Executes
ruby code and outputs HTML
In other words, your form is created (which is why there is no error shown) but not printed as HTML.
Related
to start out preemptively, I've already looked at various similar articles dealing with this, but I still get the error.
I'm starting out on rails and attempting to create a GPA calculator and tracker application for fun (and spent a lot of time searching through documentation); I have a singular controller and view since redirecting to an entire different page for calculating or saving a new GPA every time would look ugly.
Rails will display everything without error up until I add the form, no other erb is written currently, and the form is meant to submit letter grade values from the "f.selection" tag.
The culprit is #cgpa in <%= form_for #cgpa do |f| %>.
My form from main\index view:
<%= form_for #cgpa do |f| %>
<div class="field">
(...)
</div>
<div class="actions">
<%= f.submit 'Calculate' %>
</div>
<% end %>
My controller:
class MainController < ApplicationController
def index
##cgpa = CurrentGpa.all #currently calls a key_to error while form exists, otherwise no error is raised
#pgpa = PastGpa.all
#csem = CurrentSemester.all
#psem = PastSemester.all
end
def new
#cgpa = CurrentGpa.new
end
def create
(...)
end
end
The routes are simply Rails.application.routes.draw { root 'main#index'; resources :main }
If any other information is needed, just let me know to add >.>
When you use: <%= form_for #cgpa do |f| %> this automatically tries to submit the form to the CurrentGpasController create action and for doing so it sends a request to current_gpas_path. So you don't have this path in routes that is why it throwing error. Either you can add routes for CurrentGpa like:
resources :current_gpas
or you can specify a path in the form_for:
<%= form_for #cgpa, url: any_path do |f| %>
So this will submit your form to that url specified.
If you add the current_gpas routes then do create the controller and action to process your input.
And as mentioned in comments do add the #cgpa = CurrentGpa.new this in index action. The above will solve your error you are getting after that.
Hope this helps.
I have an edit view. In this view I got a dropdown and a render partial to a form. Like this:
<ul class="dropdown-menu dropdown-user installations">
<% #installations.each do |i| %>
<li>Installation<%= i.installation_id%></li>
<% end %>
</ul>
<div class="ibox-content form-installations">
<%= render :partial => 'installations/test'%>
<%= render 'form_data' %>
</div>
The view to edit the form:
<%= simple_form_for #installation, class: 'form-horizontal' do |f| %>
<%= f.error_notification %>
...
<%end%>
Controller:
def edit
#installations = current_user.installations
#installation = current_user.installations[0]
end
So in this point I can see in dropdown all installations but only can edit the first "current_user.installations[0]". So my objective is to select the installation in dropdown-menu and edit the selected installation. How I can do this?
The simplest way to do this will be to pass the relevant installation to the dropdown:
#app/controllers/installations_controller.rb
class InstallationsController < ApplicationController
def index
#installations = current_user.installations
end
end
#app/views/installations/index.html.erb
<%= render #installations %>
#app/views/installations/_installation.html.erb
<%= simple_form_for installation do |f| %>
...
<% end %>
I think there are some major issues with the structure of your code - which is why you're seeing these problems.
1. Edit
By definition edit is a member route...
This means that Rails expects a single resource to be loaded through that route (hence why you get url.com/:id/edit as the path).
The reason for this is quite simple -- Rails/Ruby are object orientated. This means that each time you create/read/update/destroy (CRUD), you're doing it to an object.
Objects are invoked by using #installation = Installation.new etc... meaning if you want to edit "all" of your installations, you'll basically need to use one of the collection routes for your Installations resource, sending any fields to the update path:
#app/views/installations/_installation.html.erb
<%= simple_form_for installation, method: :patch do |f| %>
...
<% end %>
This should send the updates to the installations#update path of your app, making it work properly.
--
2. Partials
Partials are just views which can have multiple uses; you should only use "local" variables in them.
There are two ways to invoke local scope variables into partials:
passing them in the locals: {} hash
passing them as in the as: :__ switch
In both instances, you're setting the "local" variables inside the partial to have data that was only available outside of it.
For example, you're calling:
<%= simple_form_for #installation
... inside a partial. This is bad because you're relying on #installation -- you're better using installation and populating it as you invoke the partial (as I have done in the code above).
This is the error I'm getting:
ActionView::MissingTemplate in Posts#index
Missing partial text_posts/_text_post with {:locale=>[:en], :formats=>[:html],...
Extracted source (around line #5):
3 </div>
4
5 <%= render #posts %>
Here's the code in the file app/views/posts/index.html.erb
<div class="page-header">
<h1>Home</h1>
</div>
<%= render #posts %>
I'm following along the 'Rails Crash Course' book and this is one of the steps to create a social network. I don't know the reason for the error.
I am assuming that in your posts_controller.rb file you have specified the following:
def index
#posts = Post.all
end
The reason why you are getting this error is because Rails is trying to display a partial for the variable #posts. What is implicit here, is that Rails is looking for a file named _post.html.erb in the folder app/views/posts/ and not finding anything. To fix this, you will have to manually create the file in that directory (Note that all partials begin with an underscore).
Now, if you are wondering what you should put in this partial, first you should know what <%= render #posts %> is doing. When it goes to the partial, it is iterating over all your posts and for each of them, it is going to do something. So your partial may look something like this:
<p>
<%= link_to post.title, post_path(post.id) %>
</p>
<p>
<%= link_to "Edit", edit_post_path %>
</p>
<p>
<%= link_to "Delete", post_path(post.id), method: :delete %>
</p>
Just know that what is implicit here, is that we already have a #posts.each do |post| given for us, so you only have to define whatever content you wish to display for each individual post.
Hope this helps!!
As you're using <%= render #posts %>, i'm am sure this will call the partial file with leading _ symbol on same directory. Please ensure you have already file with the name _posts.html.erb. IF you want to pass the value into partial, here i give you simple illustration :
Assume you have this in customer_controller :
def show
#customer= Customer.find(params[:id])
end
Then it instance will available on show.html.erb :
<p>Item: <%= #customer.name%></p>
These instance variables are available in layouts and partials as well, instead pass variables to partials as locals like:
// here is the same concept with your problem
// render "cust_name" find the filename leading with underscore
<%= render "cust_name", name: #customer.name%>
Above line of code will call the file with name _cust_name.html.erb that contains :
<h1><%= name %></h1>
Hope this helped.
I have the following code:
<% slika = Refinery::Page.find('sladoledi') %>
<%= link_to (image_tag slika.key_image.url, slika) %>
The problem is that it's not linking to slika. Any suggestions?
Try this format
<%= link_to(slika) do %>
<%= image_tag(slika.key_image.url)%>
<% end %>
also have a look at documentation there are nice examples how to use link_to() helper
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
First start nesting code you write here. People have problem reading code like this :(
If you want add code click code button on editor.
I suppose the problem is you don't end image_tag.
Correct form is:
link_to(image_tag(slika.key_image.url),slika)
I'm writing some Rails code for a partial view, and I want it to only show a comment field if somebody is already logged onto a site here.
If the page is viewed by someone who isn't a member of the site yet, the shared/comment_not_logged_in fragment should be passed in.
However, I'm totally stumped as to why I can't run the same check to decide if the page should add the class attribute "missing_your_voice" to the enclosing div element here:
<li class="user_submission_form bubble comment_form <% "missing_your_voice" if not current_user %>">
<% if current_user %>
<%= image_tag(current_user.avatar(:comment), :class => "profile_pic") %>
<% form_for [parent, Comment.new] do |f| %>
<%= render "comments/form", :f => f %>
<% end %>
<% else %>
<%= render :partial => 'shared/comment_not_logged_in' %>
<% end %>
</li>
The same idiom, "missing_your_voice" if not current_user returns the string in irb, and also in the console debugger.
What am I doing wrong here?
You forgot an =. Replace <% by <%=, so that you get:
<%= "missing_your_voice" if not current_user %>
Remember that <% ... %> will only run Ruby code, but not display anything. Using <%= ... %> will run the code and display the result of the expression.
As molf already pointed out, there's a missing = on your view.
It should be <%=.
Other than that, be sure to make your controller method available to your view by calling helper_method in your controller.
Take a look on the documentation if needed.