Here's the partial view I'm trying to render if an user is logged in
<div class='row'>
Post a project
<% form_for #project do |f| %>
<%= f.text_field :title, class:'form-control' %>
<%= f.text_area :brief, class: 'form-control' %>
<%= f.submit "Post" %>
<% end %>
In the main view when I try calling the partial, there's no error, but the partial doesn't render
<div class='col-lg-6'>
<% if current_user %>
<% render 'projects/new_project' %>
<% end %>
</div>
This is my current_user method, defined in the application controller. I've also defined it as a helper method to use in a view template.
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
What am I doing wrong?
You want to render, you need to output the render's returned value using <%=...:
<%= render 'projects/new_project' %>
The expression(s) within the erb tags only get evaluated without the equals sign. In order to display the result of the expression, you need to include the equals sign; place your code within <%=...%>
Similarly, for you form_for call:
<%= form_for #project do |f| %>
...
<% end %>
You need to appreciate that <%= is an output tag
This means every time you want to output something in a non-native Ruby file (.erb etc), you'll have to use <%=
This is contrary to <% which does not output anything, but still performs the functions you specify
Partial
Also, you need to ensure your partial syntax is right:
#app/views/controller/you_view.html.erb
<%= render partial: "projects/new_project" %>
#app/views/projects/_new_project.html.erb
<%= form_for #project do |f| %>
<%= f.text_field :title, class:'form-control' %>
<%= f.text_area :brief, class: 'form-control' %>
<%= f.submit "Post" %>
<% end %>
Related
Have to render the form that already exist in the project inside the page, but with a different title
app/views/welcome
<div class="form-wrapper>
<%= render 'form/form_variants/register_1', register: #register %>
</div>
-----------
app/views/form/form_variants/register_1
<%= form_for register.user, as: :user, url: register_path(service: params[:service]), method: :post %>
<%= content_tag :div, class: html_class('simpleFormHeading') do %>
<%= content_tag :h1, t('signup.title_register') %> #title that should be changed
<%= content_tag :p, raw(t('login.account_login.text', login_link: login_link)) %>
<% end %>
#a lot of inputs
<% end%>
Thanks in advance for your reply!
I would pass it as another variable to the partial
<div class="form-wrapper>
<%= render 'form/form_variants/register_1', register: #register, signup_title = 'whatever' %>
</div>
# app/views/form/form_variants/register_1
# optional: init signup_title to a default value depending on your needs
<% signup_title ||= t('signup.title_register') %>
<%= content_tag :h1, signup_title %>
I am trying to render a partial on a page in rails. For some reason the code is not being rendered into html. I have tried taking it out of the partial and just placing it in the profile page but still nothing. I am getting no errors and have restarted the server but still nothing. This is in development mode. Also all code except the message code works fine. Any help would be appreciated. Here is the code.
profile.html.erb
<% unless #pictures.nil? %>
<div id="container" style="width: 500px; height: 450px;">
<div id="slides">
<% #pictures.each do |picture| %>
<%= image_tag(picture.image.url(:thumbnail)) %>
<% end %>
<%= image_tag("left.png") %>
<%= image_tag("right.jpeg") %>
</div>
</div>
<% end %>
<% render 'message' %>
_message.html.erb
<% form_for #message, :url => messages_create_path do |f| %>
<% f.hidden_field :from, value: current_user.id %>
<% f.hidden_field :to, value: #user.id %>
<% f.text_area :message %><br />
<% f.submit "Send Message" %>
<% end %>
To make your form_for code block to render you need to use <%= tag as follows:
<%= form_for #message, :url => messages_create_path do |f| %>
<% f.hidden_field :from, value: current_user.id %>
<% f.hidden_field :to, value: #user.id %>
<% f.text_area :message %><br />
<% f.submit "Send Message" %>
<% end %>
To elaborate on vinodadhikary's answer, ERB has output tags and evaluation tags. To evaluate something you would use <% expression %>, and to output you would use <%= output.me %>. The earlier is usually used for flow control in templates, and outputs nothing. The output happens within after a decision is made. The latter is used to output stuff.
I cant get the syntax of this correct I'm trying to include :url => posts_path in the form_for section
<% form_for :post do |f| %>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %></p>
<p><%= f.submit %></p>
<% end %>
form_for expects an instance of a model, not a symbol:
<% form_for Post.new do |f| %>
...
<% end %>
There are a few ways to do this. Like #meager, you want to call Post.new. I usually do it in the controller, though.
posts_controller.rb
#post = Post.new
view
<% form_for #post do |f| %>
...
<% end %>
You can use a custom path if that's where you want to send the data. So if you have a special case, you can make a route for it
routes.rb
get '/special', to: 'posts#special'
then you can have a form that says...
<% form_tag special_path do |f| %>
...
<% end %>
and the params will pass as you'd think. Notice the form_tag instead of form_for though.
I have a rails model with a large number of fields (112), for loading a configuration. I would like, in the edit and show forms, display only if the field is already filled. That is, if the field is null in the database then do not display it for edit.
There are two records - the main one is Batch and there is a 1:1 relationship to the Primer3Batch class. I'm trying to do an edit on the Primer3Batch class on the show action of Batch, I'm not sure if this is a good idea or will even work.
I have tried using the attributes method and am getting this error:
undefined method `attributes' for #<SimpleForm::FormBuilder
batches_controller.rb
def show
#batch = Batch.find(params[:id])
#primer3 = Primer3Batch.where(:batch_id => #batch.id)[0]
respond_to do |format|
format.html # show.html.erb
format.json { render json: #batch }
end
end
batches/show.html.erb
<h1>Batch Details: <%= #batch.id %></h1>
<%= simple_form_for(#primer3) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<% f.attributes.each_attribute do |a| %>
<% if a %><%# if a is not nil %>
<%= f.input a %><%# send the field to the form %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
EDIT
Thank you JSWorld for pointing out the error of using the instance variable. I have corrected it and seem to have gotten further but it's still not quite right. This is the line changed - note attributes.each as attributes.each_attribute doesn't work.
<% #primer3.attributes.each do |a| %>
Now I am getting an error on the form fields:
undefined method `["id", 110]' for #<Primer3Batch:
I guess I need to somehow turn this :
a ["id", 110]
into:
<%= f.input :id %>
* EDIT 2 *
Final code block based on IIya Khokhryakov's answer.
<%= simple_form_for(#primer3) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<% #primer3.attributes.each_pair do |name, value| %>
<%= f.input name if value %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
I hope your intention is #primer3.attributes and not f.attributes. The error is because f is the form object and does not have attributes associated with it.
#primer3.attributes is a Hash of the model's attributes and their values. So you can do something like this:
<% #primer3.attributes.each_pair do |name, value| %>
<%= f.input name if value %>
<% end %>
these are the codes
<p>
<% form_for #movie do |m| %>
<%= m.label :title, 'Title' %>:
<%= m.text_field :title %></br>
<%= m.label :release_year, 'Release Year' %>:
<%= m.select :release_year, (1900..2011) %></br>
</br>
<% Movie.genres.each do |genre| %>
<%=h genre %>
<%= check_box_tag :genre, genre %></br>
<% end%>
</br>
<%= m.submit "Save" %>
<% end %>
</p>
and my code in the controller:
def create
#movie = Movie.new(params[:movie].merge(:genre))
if #movie.save!
render show_path
else
render new_path
end
But for whatever reason, I keep getting error messages saying "undefined method `each_pair' for :genre:Symbol" or "cannot find Movie without an ID". It's not saving properly.
Is it because of my submit form is only the movie form |m| or is it because my create function in the controller is wrong?
Thanks.
perhaps instead of
params[:movie].merge(:genre)
you wanted to do
params[:movie].merge(:genre => params[:genre])
?
Check Firebug in Firefox (or the Chrome developer tools if you're using Chrome, or your app logs if you're using neither), and see what parameters are actually being passed in your scenario? Sounds like your code is expecting to receive a parameter that is not actually being passed. The tools/log should reveal where the breakdown is happening.