0

This is my scenario I have a Stories controller that has a form to create a new Post :

stories.show.erb
...
<%=link_to("Create new post",new_story_post_path(@post))%>

This new post is generated by Posts controller

posts\new.erb
<%= form_for [@story,@post] do |f|%>

I want to break away from the traditional behavior - I want the new post form to be displayed inside the same 'story' page and NOT have a full page reload and appearing in a new page .

What's the best way to do this in rails ?

0

2 Answers 2

1

I think in your case it'll be best if you add the form in your stories show view and make it as hidden and then when a user clicks on the link to create a new post you can show them your form.

def show
  @story = Story.find(params[:id])
  @post = @story.posts.build # assuming stories have many posts
end

#show.html.erb
...
<%=link_to "Create a new Post", "#", id: "post-link"%>

<%= form_for [@story,@post], html: {id: "new-post"} do |f|%>
  // form fields
<% end %>

#some_file.js
$(document).on("click","#post-link",function(){
  $("#new-post").show();
});

#some.css.scss
#new-post{display:none;}
Sign up to request clarification or add additional context in comments.

7 Comments

I think it's a good solution but the javascript should show the hidden form , not the link ...
@Joel_Blum Can you explain why do you think javascript should show the form?
Maybe I misunderstood you , the form is hidden and you set a listener that shows $(this) which is the link to the new form (but the link is already visible and we should show the hidden form) , no?
@Joel_Blum ahh my bad didn't notice i was using this in jquery. Updated my answer
also , I had to add :remote => true to link_to to make this work
|
0

you could write:

<%= form_for [@story,@post], remote: true do |f|%>

in create.js.erb you could write

$('#post_block').html("<%= render 'your partial with posts' %>")

1 Comment

I don't Ajax is needed here and OP wants to show new post form without reload

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.